RegEx does not allow 0 to be entered by phone number

As long as I have prime "numbers" ...

/^[0-9]+$/

      

How do I do this to avoid leading zero (not starting at zero) or preg_replace, which removes all spaces and results in zero? Thank you.

+2


source to share


4 answers


/^[1-9][0-9]+$/

      



+5


source


Only numbers that do not start with 0:

/^[1-9][0-9]+$/

      

Remove all leading spaces and zero:



$num = preg_replace('/^(?:0|\s)*([0-9]+)$/', '\1', ' 0999');

      

To remove all whitespace in a string, as well as those not maintained, use str_replace. This can be done with a regex, but if you are going to loop a lot of numbers that will be slower.

+4


source


/^[^0][0-9]+$/

      

0


source


(^([1-9]{1})+([0-9]{9})+$

      

This code for a phone number is 10 characters long and does not start with zero.

0


source







All Articles