IntelliJ Java IDE
The moose likes HTML, CSS and JavaScript and the fly likes ways to validate Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "ways to validate" Watch "ways to validate" New topic
Author

ways to validate

Jay Richards
Ranch Hand

Joined: Jan 16, 2003
Posts: 58
i need your javascript experties guys...
can i validate an expression like this
# numbers
[(####)] ###[-]#### [loc ####]
the optional [(####)] can be 2 - 4 numbers
the optional [loc ####] can be 3 - 4 numbers
i beleive i can do it on perl but i can't squeeze my brain on how to do it on javascript.
thanks anyone. hope you could post some scripts...
thansk again...
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50673

Look up regular expressions. That should get you on-track.
hth,
bear


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
I am guessing you wanted the brackets in it?
So here is what I came up with.
<script>
var TheNum="[(111)] 111[-]1111[loc111]";
var ThePat = /^\[\(\d{3}\)\] \d{3}\[-\]\d{4}\[\d{2,4}\]$/;
var ThePat2 = /^\[\(\d{3}\)\] \d{3}\[-\]\d{4}\[loc\d{3,4}\]$/;
if(ThePat.test(TheNum) || ThePat2.test(TheNum) ){alert('match');}
else alert('no');
</script>

Eric
 
 
subject: ways to validate
 
Threads others viewed
Validating email in javascript?
validation for a numeric field
E-mail address validation
Special characters in Java
Javascript validtaion problem
MyEclipse, The Clear Choice