Knock Knock
Help again..
Whats the regex for the format like
883 02 12313545
or
883-02-12313545
| SitePoint Sponsor |
Knock Knock
Help again..
Whats the regex for the format like
883 02 12313545
or
883-02-12313545





Just replace the \s with a - for the second case.Code:var nr = "883 82 12313545"; var regExp = /^\d{3}\s\d{2}\s\d{8}$/; if(regExp.test(nr)) { alert("Number is fine"); } else { alert("Not fine"); }


Pepejeria,
I wrote this:
Message box displays "Not fine".Code:var regExp = /^\d{3}\s\d{2}\s\d{8}$ | ^\d{3}\-\d{2}\-\d{8}$ /
Message box displays "Number is fine".Code:<html> <head> <script type='text/javascript'> var nr = "883 82 12313545"; var regExp = /^\d{3}\s\d{2}\s\d{8}$/ ; var regExp2 = /^\d{3}\-\d{2}\-\d{8}$/; if(regExp.test(nr) || regExp2.test(nr) ) { alert("Number is fine"); } else { alert("Not fine"); } </script>
Last edited by muazzez; Apr 20, 2007 at 05:02.





Skip the spaces around the | and you should be fine with one regular expression to test both cases
Code:var nr = "883-82-12313545"; var regExp = /^\d{3}\s\d{2}\s\d{8}$|^\d{3}-\d{2}-\d{8}$/; if(regExp.test(nr)) { alert("Number is fine"); } else { alert("Not fine"); }
The following regular expression will make sure that it works both ways plus it will also allow you to use combination off space and - in the string
Code:/^\d{3}(?:\s|\-)+\d{2}(?:\s|\-)+\d{8}$/


Pepejeria and Mortimer,
It worked very well. Thanks...
Last edited by muazzez; Apr 20, 2007 at 05:52.
Bookmarks