Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

international cell phone format

Last post 02-05-2006, 11:41 PM by ojoonline. 9 replies.
Sort Posts: Previous Next
  •  02-04-2006, 8:12 PM 15035

    international cell phone format

    hi,

    I need two similiar patterns.  One to check for a string of n digits to make sure that the first digit is not zero (0).

    [1-9] [0-9] [0-9][0-9][0-9][0-9][0-9][0-9]etc

    Then, I need to be able to have a comma delimited list of the above pattern.

    [1-9] [0-9] [0-9][0-9][0-9][0-9][0-9][0-9],[1-9] [0-9] [0-9][0-9][0-9][0-9][0-9][0-9]etc

    thanks!

    jo

  •  02-04-2006, 8:39 PM 15037 in reply to 15035

    Re: international cell phone format

    You were not very clear, so if my regexes are not what you were hoping for please explain in more detail and I will try to help.

    This will match numbers of any length which do not start with 0:

    \b(?!0)\d+\b

    As for returning the matches in a comma-delimited list, you can replace each match with:

    \0,

    My regex-centric blog :: JavaScript regex tester
  •  02-05-2006, 1:41 AM 15040 in reply to 15037

    Re: international cell phone format

    hi Stevezilla00,

    thank you for your response.  the first part works beautifully... thanks.  i'm not sure how to use the next part though.  here's my code:

    <script language="JavaScript">
    function validateString(obj, formObj, formValue) {
    var regex = /\b(?!0)\d+\b/;
    return regex.test(formValue);
    }
    </script>

    and here's a working example-> http://www.textit.com.au/x-testingJS.cfm

    I need 61401345678,34678903,3489506023,23456677 to pass and

    61401345678,34678903,0489506023,23456677 to NOT pass.

    thanks for this.  your help is appreciated.

    jo

  •  02-05-2006, 7:59 AM 15041 in reply to 15040

    Re: international cell phone format

    try

    ^([1-9]\d*,)+[1-9]\d*$

    61401345678,34678903,3489506023,23456677 should pass

    61401345678,34678903,0489506023,23456677 will not

    there should be no less than 2 numbers delimited by a comma for this to work

    Sergei

  •  02-05-2006, 12:44 PM 15042 in reply to 15040

    Re: international cell phone format

    Okay, I understand better now.

    Sergei Z's regex is good. One thing, I'm pretty religious about putting anything in parens that I don't need captured as a backreference into a non-capturing group, like so:

    ^(?:[1-9]\d*,)+[1-9]\d*$

    It will run just a tiny, tiny bit faster.

    Two things about my previous regex...
    1. The construct above without the negative lookahead will run a bit faster in some cases.
    2. It looks for a number starting and ending at a word boundary, not at the very beginning and end of a string (e.g. "cat 123 dog" will return 123 instead of failing the match).

    To address both these issues, change \b(?!0)\d+\b to this:

    ^[1-9]\d*$
    My regex-centric blog :: JavaScript regex tester
  •  02-05-2006, 6:04 PM 15045 in reply to 15042

    Re: international cell phone format

    thank you, thank you, thank you!  I'll give them all a shot and see what happens.

     

    jo 

  •  02-05-2006, 6:29 PM 15046 in reply to 15045

    Re: international cell phone format

    hi,

    The single ones both work, but \b(?!0)\d+\b allows spaces and ^[1-9]\d*$ does not.  no spaces is better i think.

    For the multiple comma delimited one, is it possible to check for one instance as well as multiples?

    For example:

    33434353434 should pass

    or

    33434343234,23234565565,2324235453,2234234343 should pass

    thanks so much,

    jo

  •  02-05-2006, 9:12 PM 15050 in reply to 15046

    Re: international cell phone format

    try

    (?:^(?:[1-9]\d*,)+[1-9]\d*$)|^[1-9]\d*$

    it'll validate both

    33434353434

    and

    33434343234,23234565565,2324235453,2234234343

  •  02-05-2006, 10:47 PM 15051 in reply to 15050

    Re: international cell phone format

    alternatively try

    ^[1-9]\d*(?:(?:,[1-9]\d*)+)?$

  •  02-05-2006, 11:41 PM 15053 in reply to 15051

    Re: international cell phone format

    hi Sergei,

    thanks!  that works a treat :-)

    jo

View as RSS news feed in XML