want to do a phone validation more how do I validate the dash also if the user puts?

Asked

Viewed 294 times

1

This is what I’ve got so far:

public static function ValidaFone($RegeEx)
{
 $RegeEx = preg_match('/^[0-9]{9,11,13}$/',$RegeEx);

      ........code.....
}
  • What is the expected format?

  • first time I use regex as I do to say that the ninth digit in case the dash may be optional ?

  • 1

    The dash is something 'optional' and 'personal', after adding another nine, I’ve seen people writing the tel with two strokes: 9-9999-9999. Remove the dash and save only the numbers.

  • expected format is that the user type the phone I validate if he type area code , or only phone or with the most accurate country code validate in regex also exactly as he said leaving the optional dash if he type or not more as I do this ?

1 answer

1


Phone traces can only be present in specific positions, right?

/^([0-9]{5}(-| )?[0-9]{4})$/

This regex says the following:

  • five numbers between zero and nine;
  • an optional space or hyphen;
  • four numbers between zero and nine.

You can refine the expression to not allow, for example, zero-initiated phone numbers, or to include more snippets like area code.

To understand any regular expression in JS, recommend the site http://regex101.com/. You can put an expression there and the site explains the excerpts.

  • Thanks @Renan thinking so have you given me an idea this way not valid case user also enter the code of the country that would be two more character but gave me a good idea for this worth

Browser other questions tagged

You are not signed in. Login or sign up in order to post.