Reputation: 1553
I am working on a registration form, where i am providing validation(spring) for input (user)data. I have fields like
Name (Full Name)
Only Characters are allowed
-> Acceptable values are : A, AB, A B, A B C, Abc def, abc def ghi
-> No Junk/special characters are allowed.
Regular expression i am currently using :
Pattern.compile("(([a-zA-z])+([\\s]+[\\s])?[^*$&!@%~\",:;{}|=()_0-9])*");
-> string with single and two spaces are working, which is fine.
-> only numbers not accepting, which is fine.
-> pattern doesn't work with "single character", like "a" or "A"
-> not throwing an error on entering Alphanumeric, like abc23
Income
Which should accept strictly only numerical values
Regular expression i am currently using :
Pattern.compile ("(([0-9])?[^*&!@%~\",:;{}|=()_a-zA-z])*")
problem
accepting alphanumeric
Registration No
it can be a character, can be a number, can be aplhanumeric, but no space is allowed
Can anyone please help me in writing the correct regexp (only regexp works) for the above mentioned fields?
Upvotes: 0
Views: 4409
Reputation: 1656
Your regexps are long which leads me to think there may be additional constraints. However, given the bullet points you've given...
For the name, you can use:
Pattern.compile("[a-zA-Z ]+")
For income, you can use:
Pattern.compile("[0-9]+")
For registration, you can use:
Pattern.compile("[a-zA-Z0-9]+")
Upvotes: 3