Feha
Feha

Reputation: 314

RegEx don allow leading 0 in phone number

So far I have simple "numbers" only ...

/^[0-9]+$/

How can it be done to not allow leading zero (not start with zero) or preg_replace that would remove all spaces and leading zero ? Thank You

Upvotes: 2

Views: 8239

Answers (4)

nukemmm
nukemmm

Reputation: 1

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

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

Upvotes: 0

OIS
OIS

Reputation: 10033

Only numbers not starting with 0:

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

Remove all leading spaces and zero:

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

To remove all spaces in string, also those not leading, use str_replace. It can be done with regex, but if you are going to loop many numbers that would be slower.

Upvotes: 4

brettkelly
brettkelly

Reputation: 28205

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

Upvotes: 0

Eimantas
Eimantas

Reputation: 49344

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

Upvotes: 5

Related Questions