How to check whether a number is two digit number in java swing using regular expression validator
Here you can see how rollnumber is verified
package swing;
public class RollNumberValidatorImpl extends InputVerifier{
public boolean verify(String input) {
boolean isRollNumber = false;
String rollNumber = input;
if(rollNumber!=null && rollNumber.length()>0){
isRollNumber = rollNumber.matches("\\d{1,2}")? true: false;
}
return isRollNumber;
}
public static void main(String[] args) {
RollNumberValidatorImpl r = new RollNumberValidatorImpl();
System.out.println(r.verify("01"));
}
}
+Java
How to create a JTextfield in java that accepts only digits
The code verify for four digit number in java textfield First of all you need to create a class that extends inputverifier, then you have to register this class with your text field
class test{
public static void main(string arg){
JTextField motorolaText = new JTextField();
motorolaText.setInputVerifier(new FirmCodeValidatorImpl());
}
}
The above code will shows you how to call the validation class, and the below one is validation class, i used regular expression here to validate the input
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;
public class FirmCodeValidatorImpl extends InputVerifier{
@Override
public boolean verify(JComponent input) {
boolean isFirmCode = false;
String firmCode = null;
JTextField textField = (JTextField) input;
if(textField!=null){
firmCode = textField.getText();
isFirmCode = firmCode.matches("\\d{4}")? true: false;
}
return isFirmCode;
}
}
How to check whether a number is two digit in java
if you are satisfied with the code please provide your comments
http://belazy.blog.in/
Thanks very interesting blog!
ReplyDeleteHere is my blog post: bet angel
Thanks for your comment
Delete