views:

57

answers:

4

hello everyone,

I want to get the length of integer values for validation in PHP. Example: Mobile numbers should be only 10 integer values. It should not be more than 10 or less than 10 and also it should not be included of alphabetic characters.

How can i validate this.

Sorry for my poor English.

Thanks In Advance

+5  A: 
if (preg_match('/^\d{10}$/', $string)) {
  // pass
} else {
  // fail
}
Alex Howansky
+1 cus it works....
Shadi Almosri
Doing $string = preg_replace('/\D/', '', $string); first would make it more allow people to enter spaces and pluses etc without throwing errors.
BenWells
thanks its working
GitsD
+1  A: 
$num_length = strlen((string)$num);
if($num_length == 10) {
    // Pass
} else {
    // Fail
}
mdm
A: 
$input = "03432 123-456"; // A mobile number (this would fail)

$number = preg_replace("/^\d/", "", $number);
$length = strlen((string) $number);

if ($number == $input && $length == 10) {
    // Pass
} else {
    // Fail
}
Matt Kirman
A: 

If you are using a web form, make sure you limit the text input to only hold 10 characters as well to add some accessibility (users don't want to input it wrong, submit, get a dialog about their mistake, fix it, submit again, etc.)

Matt Bell