Jump to content

Variable validation - numbers only


daveh33

Recommended Posts

The code I am using is

 

$number = $_POST['UserMobile'];
if(ereg("^[0-9]{19}$", $number)) {
$mobile = $number;
}
else {
echo "Invalid phone number - please go back and try again<P>";
}

 

This works - but if the number is less than 19 digits it displays the "invalid phone number.." I want it so it accepts it if its up to 19 digits

 

What changes need to be made to if(ereg("^[0-9]{19}$", $number)) {

Link to comment
https://forums.phpfreaks.com/topic/74444-variable-validation-numbers-only/
Share on other sites

Check the strlen() manual. and the eregi() manual

 

i think this is what you were trying too do with the code:

 

$number = $_POST['UserMobile'];
if(ereg("^[0-9]{20}$", $number)) {
$mobile = $number;
}
else {
echo "Invalid phone number - please go back and try again<P>";
}

The form is requesting an number in international format and is just not restricted to the UK - therefore UK numbers start 447 , but to allow  other international mobile numbers to be accepted the form needs to handle up to 19 digits.

 

^[0-9]{1,19}$ works

 

Thanks for that code

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.