Regex for Mobile Number Validation

前端 未结 3 1847
别那么骄傲
别那么骄傲 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:29

    This regex is very short and sweet for working.

    /^([+]\d{2})?\d{10}$/

    Ex: +910123456789 or 0123456789

    -> /^ and $/ is for starting and ending
    -> The ? mark is used for conditional formatting where before question mark is available or not it will work
    -> ([+]\d{2}) this indicates that the + sign with two digits '\d{2}' here you can place digit as per country
    -> after the ? mark '\d{10}' this says that the digits must be 10 of length change as per your country mobile number length

    This is how this regex for mobile number is working.
    + sign is used for world wide matching of number.

    if you want to add the space between than you can use the

    [ ]

    here the square bracket represents the character sequence and a space is character for searching in regex.
    for the space separated digit you can use this regex

    /^([+]\d{2}[ ])?\d{10}$/

    Ex: +91 0123456789

    Thanks ask any question if you have.

提交回复
热议问题