Regex for Mobile Number Validation

前端 未结 3 1839
别那么骄傲
别那么骄傲 2020-12-05 02:38

I want a regex for mobile number validation. The regex pattern should be such that it must accept + only in beginning and space(or -) should be al

3条回答
  •  执笔经年
    2020-12-05 03:28

    Satisfies all your requirements if you use the trick told below

    Regex: /^(\+\d{1,3}[- ]?)?\d{10}$/

    1. ^ start of line
    2. A + followed by \d+ followed by a or - which are optional.
    3. Whole point two is optional.
    4. Negative lookahead to make sure 0s do not follow.
    5. Match \d+ 10 times.
    6. Line end.

    DEMO Added multiline flag in demo to check for all cases

    P.S. You really need to specify which language you use so as to use an if condition something like below:

    // true if above regex is satisfied and (&&) it does not (`!`) match `0`s `5` or more times
    
    if(number.match(/^(\+\d{1,3}[- ]?)?\d{10}$/) && ! (number.match(/0{5,}/)) )
    

提交回复
热议问题