S Jagdeesh
S Jagdeesh

Reputation: 1553

Data Validation - Regular expression

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.

-> 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

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

Answers (1)

brandon
brandon

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

Related Questions