Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

problem about using javascript to valide data

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javascriptkit.com/jsref/regexp.shtml

Eric
 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic