How to create a regular expression to validate just a mobile number?

2

I made this expression, but I do not know if it is in today's patterns

 "/^[1-9]{2}\s?9\s?\d{8}$/"
    
asked by anonymous 21.06.2017 / 18:46

2 answers

1

Regarding the question, I think you should be a bit more specific, your title does not match the description of the question.

Responding to the title:
You can create a regex that reads the digits separately and only matches if the number is 8-9 digits with 2 additional digits to indicate DDD. So you can use something like this:

(\({0,1}\d{0,2}\){0,1} {0,1})(\d{4,5}) {0,1}-{0,1}(\d{4})

This regex identifies whether there are tabs such as space, "-" and "()", as they are not required attributes, it only considers that they can exist and capture the same way, here is a example of how it works .

Responding to the question description:
Your regex still works, it's in today's standards, but you would not use it for not considering the possible tabs that may exist in the phone fields, being stuck to possibilities that should be at the beginning of lines, and accepting breaks in lines, tabs, and spaces between specific parts that may end up capturing sequences of characters that should not be captured.

    
21.06.2017 / 20:32
0

Hmmm maybe this expression works, it worked for some tests with 9 digits and 8 digits, with or without ddd, and with or without hyphen

var cel = /[0-9]{0,2}?[9]?[0-9]{4}-?[0-9]{4}/;
cel.test('91234-5678') // true
    
21.06.2017 / 21:39