Validate phone number and email in php

Phone number validation using regular expression

$user_phone = $_POST['user_phone'];

if(!preg_match('/^([0-9]+)$/', $user_phone))
{
    $_SESSION['info']="Please enter valid phone number";
    header('location:/register');
}

Phone number validation using is_numeric function

$user_phone = $_POST['user_phone'];
if(!is_numeric($user_phone))
{
    $_SESSION['info']="Please enter valid phone number";
    header('location:/register');
}
if (filter_var($user_email, FILTER_VALIDATE_EMAIL) == false){
    $_SESSION['info']="Please enter valid email";
    header('location:/register');
}
  • is_int or is_integer
  • is_numeric
  • regular expressions
  • ctype_digit
  • filter_var

is_integer()

for this function these values are are not valid: “0010”, “123”

is_numeric()

for this function these values are valid: 1.3, +1234e44 and 0x539

filter_var()

for this function a value as “00123” is not valid

CONSLUSION

it seems that only regex and ctype_digit work always fine.



Leave a Reply