SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    whats the regex for this express ??

    Knock Knock
    Help again..
    Whats the regex for the format like

    883 02 12313545

    or

    883-02-12313545

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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");
    }
    Just replace the \s with a - for the second case.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pepejeria,
    I wrote this:
    Code:
    var regExp = /^\d{3}\s\d{2}\s\d{8}$  |    ^\d{3}\-\d{2}\-\d{8}$ /
    Message box displays "Not 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>
    Message box displays "Number is fine".
    Last edited by muazzez; Apr 20, 2007 at 04:02.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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");
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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}$/

  6. #6
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pepejeria and Mortimer,
    It worked very well. Thanks...
    Last edited by muazzez; Apr 20, 2007 at 04:52.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •