This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes problem about using javascript to valide data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem about using javascript to valide data " Watch "problem about using javascript to valide data " New topic
Author

problem about using javascript to valide data

david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
http://www.javascriptkit.com/jsref/regexp.shtml

Eric
david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Apr 10, 2009
Posts: 133
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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem about using javascript to valide data
 
Similar Threads
how to prevent ice:commandButton from submitting if there is a validation error
backslash is removed from the parameter when javascript function get the parameter from java call
Problem with http request and external javascript
Regular Expression Help
Detecting that a String represents a valid number