javascript – How to create a regular expression to validate cell number only?

Question:

I made this expression, but I don't know if it's up to today's standards.

 "/^[1-9]{2}\s?9\s?\d{8}$/"

Answer:

Regarding the question, I think you should be a little more specific, its title doesn't match the description of the question.

Replying to the title:
You can create a regex that reads the digits separately and only matches if the number has 8-9 digits with 2 additional to indicate area code.
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 if there are separators such as space, "-" and "()", as they are not mandatory attributes it just considers that they may exist and captures the same way, here is an example of how it works .

Answering the question description:
Your regex still works, it's up to today's standards, but I wouldn't use it because it doesn't consider the possible separators that might exist in phone fields, it's stuck with possibilities that must be at the beginning of lines, and it accepts line breaks, tabs and spaces between specific parts that might end up capturing strings that shouldn't be captured.

Scroll to Top