Hi i have a problem where i need to put a validation on the text box that it contains only alphabets and numbers. For this i m using pattern and matcher.
I want this validation to be applied to the java file only and not on jsp page page.
My code looks like this
How can i make sure that the text contains only a-z n 0-9 only. becoz whenever i enter any normal text with special characters it accepts on clicking the save button.
I want to write a pattern that returns true only if the securityAnswer string contains alphabets and numbers and not anything else.
Pattern pattern = Pattern.compile("([^a-z A-Z 0-9])");
String original = "%asfSD!@";
Matcher match = pattern.matcher(original);
Thanks for the help!
Hate to break the news to you -- but you really should learn regexes, if you are going to use them. All that change does is to make sure that the first character is a number, letter, or space. You can still have other characters, as long as it is not the first character (BTW, I am assuming that you are still using find()).
I v tested this in number of ways by taking the input string as
the matcher.find() is returning the following results:
"%)Aas(fSD" returns true // it means the input string contains other than characters n digits
"Aas(fSD" returns true // it means the input string contains other than characters n digits
"AasfSD" returns false // it means the input string contains characters n digits
"asfSD" returns false // it means the input string contains characters n digits