0

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.

  • problems

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

NullUserException
  • 83,810
  • 28
  • 209
  • 234
S Jagdeesh
  • 1,523
  • 2
  • 28
  • 47
  • 3
    In [tchrist](http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/6163129#6163129)'s words: "Code that believes someone’s name can only contain certain characters is stupid, offensive, and wrong." – NullUserException Aug 26 '11 at 18:02
  • 1
    if that's the requirement, you're certainly helpless. tchrist knows this. – S Jagdeesh Aug 26 '11 at 18:22
  • +1 In a previous project, one of our users reported a bug under the title "No Irish Need Apply". You can probably guess what the issue was. Needless to say we were embarrassed. – Dan Aug 27 '11 at 20:54

1 Answers1

3

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]+")
NullUserException
  • 83,810
  • 28
  • 209
  • 234
brandon
  • 1,585
  • 1
  • 10
  • 13