Reputation: 365
I have a drop down and a text box beside that, if 2nd or 3rd element selected in drop down then the text box beside that drop down should accept only number or word or date,no special character is allowed. Can any one provide server side java code validation for this?
if(badRowDetails.getOperator()==2 || badRowDetails.getOperator ()==3)
{
char c[]=value.toCharArray ();
int count=0;
int count1=0;
for(int i=0;i<value.length ();i++)
{
int ascii=(int)c[i];
if((ascii >=97 && ascii<=122) || (ascii>=65 && ascii<=90))
{
count=count+1;
}
}
try
{
int num=Integer.parseInt(value);
}
catch(NumberFormatException nfe)
{
System.out.println ("Exception raised . "+nfe.getMessage ());
count1=count+1;
}
if(count==0 && count1>0)
{
errors.reject ("value","Operator IS , ISN'T can only have words,Numbers.");
}
}
}
thank you
Upvotes: 1
Views: 761
Reputation: 36304
public static void main(String... args) {
String s = "12:12:1990";
System.out.println(s.matches("(\\d+|\\w+|([0-2][0-9]|([3]([0]|[1]))):([0][1-9]|[1][1-2]):([1][9][0-9][0-9]|[2][0][0-9][0-9]))"));
}
PS : date has delimiter ":" (assumed) and ranges between (1900 and 2099). You can change it to whatever delimiter you want by replacing ":" with your delimiter in the code. Doesnt handle LEAP YEAR condition...
IP:Hello OP : true
IP :123 OP : true
IP :12:12:2011 Op :true
IP: 12:44:2012 OP :false
IP :][ OP :false
IP : 32:12:2013 OP : false
Upvotes: 1
Reputation: 3679
For Alpha Numeric, I would prefer to validate using regular expression, use DateFormatter
to check a date (since it needs lot of validation such as leap year, month,date, year etc)
Here you go quick example how I do it, public static void main(String[] args) {
String s1="111";
String s11 ="11aa*";
String s2="hello";
String s3="2013-05-11";
System.out.println("Is number 111 "+isAlphaNum(s1));
System.out.println("Is number 11aa* "+isAlphaNum(s11));
System.out.println("Is word hello "+isAlphaNum(s2));
System.out.println("Is date 2013-05-11 "+isaDate(s3));
System.out.println("Is date 111 "+isaDate(s1));
System.out.println("Is date hello "+isaDate(s2));
}
private static boolean isAlphaNum(String word){
return word.matches("^[a-zA-Z0-9]*$");
}
private static boolean isaDate(String text){
if (text == null || !text.matches("\\d{4}-[01]\\d-[0-3]\\d")){
return false;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
try {
df.parse(text);
return true;
} catch (ParseException ex) {
return false;
}
}
You may change the date format globaly(dynamic) as you require
Upvotes: 0
Reputation: 6557
Use Regex as below:
if(badRowDetails.getOperator()==2 || badRowDetails.getOperator ()==3)
{
if(!value.matches("[A-Za-z0-9-]*")) {
errors.reject ("value","Operator IS , ISN'T can only have words,Numbers.");
}
}
Upvotes: 0
Reputation: 494
Using regex like the following will return true if the string is alphanumeric:
Pattern pattern = Pattern.compile("^[a-zA-Z0-9]*$");
Matcher matcher = pattern.matcher(s);
if (matcher.find()){
return true;
}
return false;
Date is tougher. I'd lookup regex for validating date.
Upvotes: 0