Jump to content

[SOLVED] Validate variable - Numbers only


daveh33

Recommended Posts

$number = $_POST['UserMobile'];
if(ereg("^[0-9]{11}$", $number)) {
$mobile = $number;
}

 

I use the above code at the moment to validate the input of a users mobile - the code above basically just checks that the variable is numeric only and has 11 digits.

 

How can it be developed so that it also checks that the number starts 07?

Link to comment
https://forums.phpfreaks.com/topic/84267-solved-validate-variable-numbers-only/
Share on other sites

<?php

$usermobile = $_POST['UserMobile'];

if(strpos($usermobile, '07') === 0 && ctype_digit($usermobile))
{
echo 'Good number';
}
else
{
echo 'bad number';
}

?>

 

Bit more bloated from a code perspective, but it's faster than using regex functions. If your site gets busy, this will be more efficient (in the long run)

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.