Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem about using javascript to valide data

 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I just learned regex, i tried to use javascript .test function to validate the following pattern but failed, i was told they are kind of java regex, but i do not know what is difference between java regex and javascript regex, please help:

1. [\p{Alpha}\.]{0,6} to validate GENDER ,it means that 0 to 6 number is allowed, right?
2. [\p{Alpha}\._ -]{0,31} to validate STATE ,0 to 31 number will be fine, right?
3. [\p{Alnum}\. -]* to validate ZIP CODE, nubmer and alpha will be fine, right?
4. \+?[\d\.() -]{0,28}[\p{Alpha}\.\+ ]{0,6}?\d{0,4}? to validate PHONE NUMBER.


I used the following JAVASCRIPT code:



when i input #%×%% as value,all the above did not show alert, which suppose only allow number and alphabet, could anyone help to figure out what is wrong with it, thanks a lot for help!!
 
Eric Pascarello
author
Rancher
Posts: 15385
6
 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Eric. I happened reading the link today earlier when I searched internet.

I tried both method to define the regex:

regex_pattern = /..../
regex_pattern = new RegExp(pattern)

Someone told me that p{Alpha} is java specific regex and I can not use javascript to validate it, I want to make sure of it since i know little about regex thing. I know regex is independent, but i do not know what difference between java regex and javascript regex (perl/unix). Since this is kind of ergent, I do not have much time to learn and figure it out, so post it here, wish someone can help me out.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why would you use a reg expression for gender? very odd
State, you should have a list because "FOobar" would work

You can easily get what you want with google
http://www.google.com/search?q=javascript+phone+number+regex
http://www.google.com/search?q=javascript+zipcode+regex

Eric
 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Eric! I got these regex pattern from server part for validation, but i want to validate at client side first and I need to be consistent about the validation no matter they are good or not.

I have swiched using java to validate the data, thank you for your information and help anyway. I really appreciate!

Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic