Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Mobile-Applications TextField validations
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


kalyani
TextField validations
1 Answer(s)      3 years and a month ago
Posted in : MobileApplications


I want to know How to check first letter capital or not in a text field and also check all are characters or not in same textfield?

and also want to know how to check all digits or not in a textfield? ex:when we enter data in age field we want check they are digits or not?


Advertisement
View Answers

August 23, 2011 at 12:15 PM


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;
class  ValidateData{

    public static void main(String[] args){
    JFrame f=new JFrame();
    JLabel label1=new JLabel("Name: ");
    JLabel label2=new JLabel("Age: ");
    final JTextField text1=new JTextField(20);
    final JTextField text2=new JTextField(20);
    JButton b=new JButton("Check");
    text1.addKeyListener(new KeyAdapter(){
        public void keyReleased(KeyEvent e){
            String st=text1.getText();
            char chr = st.charAt(0);
            boolean check = Character.isUpperCase( chr);
         Pattern p = Pattern.compile("^[a-zA-Z]+$");
      Matcher m = p.matcher(st);
      if(!m.find()){
      JOptionPane.showMessageDialog(null, "Only characters A-Z, a-z are allowed!","Error!", JOptionPane.ERROR_MESSAGE);
      text1.setText("");
      }
      else if(!check){
          JOptionPane.showMessageDialog(null, "First character should start with capital letter!","Error!", JOptionPane.ERROR_MESSAGE);
          text1.setText("");
      }
     }
    });
    text2.addKeyListener(new KeyAdapter(){
        public void keyReleased(KeyEvent e){
            String st=text2.getText();
         Pattern p = Pattern.compile("(\\d)");
      Matcher m = p.matcher(st);
      if(!m.find()){
          JOptionPane.showMessageDialog(null, "Only numbers are allowed!", "Error!", JOptionPane.ERROR_MESSAGE);
       text2.setText("");
      }
     }
    });
    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        String v1=text1.getText();
        String v2=text2.getText();
        JOptionPane.showMessageDialog(null,"Name is: "+v1+", Age is: "+v2);
                 }
        });
        JPanel p=new JPanel(new GridLayout(3,2));
        p.add(label1);
        p.add(text1);
        p.add(label2);
        p.add(text2);
        p.add(b);
        f.add(p);
        f.setVisible(true);
        f.pack();
    }
}


Related Tutorials/Questions & Answers:
TextField validations
TextField validations  I want to know How to check first letter capital or not in a text field and also check all are characters or not in same textfield? and also want to know how to check all digits or not in a textfield? ex
TextField validations
TextField validations  I want to know How to check first letter capital or not in a text field and also check all are characters or not in same textfield? and also want to know how to check all digits or not in a textfield? ex
Advertisements
validations
validations  How to get All Client and server side validations in spring using jsp   Please visit the following link: Spring Validation Example
Validations
Validations  Hi I need to validate blank text field that can accepts only integers. Here is my code if(textField_1.getText().length()==0) JOptionPane.showMessageDialog(null, "enter text in textfield"); I wrote like
validations in javascript
validations in javascript  How do you check validations in javascript
Spring Validations
Spring Validations  Hi.. i need code for validations using spring annotations
validations - Struts
validations  log in page with validations user name must be special character and one number and remining is alphabetes in struts
TextField on the Image
TextField on the Image  how we set label or textfield etc. on the Image
TextField
TextField      ... as the TextField. In the example given below, shown a textfield in the applet by creating its...;TextField("Type in the box");    add(tf); 
Struts Validations - Framework
Struts Validations  What is the server-side validations exactly? give me on example
Multiple Validations on jTestFields
Multiple Validations on jTestFields  Hi, I am working on an window application in which I have to validate the data of text fields, like if textbox1... then it should check whether it is valid or not. Unless all the validations
Custom Validations - Struts
Custom Validations  Hi, I am trying to do the custom validations in struts 1.1.That is how can we plug in our own custom validations into Validator ? Can anybody tell me how to do this with a sample example. Thanks
listbox and textbox validations
listbox and textbox validations  hi, any one please tell me how to set validations for listbox and textboxes using bean classes? thank you   Please visit the following link: http://www.roseindia.net/jsp/user
struts validations - Struts
struts validations  hi friends i an getting an error in tomcat while running the application in struts validations the error in server as validation disabled plz give me reply as soon as possible.  Hi friend
validations in struts - Struts
validations in struts  hi friends plz give me the solution its urgent I an getting an error in tomcat while running the application in struts validations the error in server as "validation disabled" plz give me reply as soon
textfield selected text
textfield selected text  How to select text in text field
Java textfield validaton
Java textfield validaton  Sir... i'm new to java. In java JTextfield when we entered a negative value after pressing one button it have to display an error message dialog of textfield and also the textfield should accept
Java textfield validaton
Java textfield validaton  Sir... i'm new to java. In java JTextfield when we entered a negative value after pressing one button it have to display an error message dialog of textfield and also the textfield should accept
s:textfield - Struts
s:textfield  I am using the s:textfield tag in an appication and need... this?  Hi friend, Textfield Tag Example Textfield Tag Example
Im not getting validations - Struts
Im not getting validations  I created one struts aplication im using DynaValidations I configured validation.xml and validation-rules.xml also in s-c.xml But Im unable to get validations This is my validation.xml file
Javascript validations problem
Javascript validations problem  i have attached two jsp files 1.sports1.jsp and sports.jsp files Here when i run my sports.jsp file my validation works correctly but when my validation fails i am getting an alert window and i am
IPhone-UIPicker Data to TextField
IPhone-UIPicker Data to TextField  When I select the textfield, a picker view must come in form of action sheet and the data selected from that picker view must be populated in the text field. can anyone help me on this please
get textfield value in javascript
get textfield value in javascript  How to get and the set value of text field in JavaScript?   Get the text field value in JavaScript <html> <head> <script type="text/javascript"> <
how to set the value of textfield constant
how to set the value of textfield constant  how to set the value of textfield constant
select box and text box validations
select box and text box validations  hi, any one please tell me how to set validations for select box and text boxes using bean classes? thank you   Please visit the following link: http://www.roseindia.net/jsp/user
Not retriving the validations from validation.xml - Struts
Not retriving the validations from validation.xml  I have create the file called register.jsp    LoginID: Password: when i
java textfield problem
java textfield problem  I want to navigate the content of text field from one class to another and viceversa through button but when I am getting the text from first class I am getting null value.please help me..here is the code
how to set size of button,textfield.
how to set size of button,textfield.  how we set size of button textfield etc and reduce problem when click maximize button the text or textfield get large how to set as a preferd size in the panel or frame .when we click
How to Type in japanese in java Textfield?
How to Type in japanese in java Textfield?  Hello Sir/madam; Am creating an application in java in which i want to get a japanese String input from... in textfield ..Please help me and tell me how to write in japanese
how to move pointer in Textfield to newline
how to move pointer in Textfield to newline  consider my case...; class Table1 extends Frame implements ActionListener{ TextField tf1,tf2,tf3..."); l2.setBounds(100,50,200,20); tf1=new TextField(); tf1.setBounds(100,80,200,20
Advertisements
 

 

 

DMCA.com