I recently created a PHP form (my first one) and I wanted to have a phone number validation feature. Now, I have written the code. But it works for two or three runs and then stops working all together. I don't get any errors. It simply accepts anything that I type in. The rest of the validations (name and radio button entries) work without a problem. I am checking for a 10-digit number (no special characters). The validation must check the length of the entry and should ensure only numbers are entered.
What am I doing wrong here?
if (empty($_POST["mobile"])) {
$mobileerr = "Mobile number is required";
} else {
$mobile = test_input($_POST["mobile"]);
if (preg_match('/[^0-9]{10}/', $mobile)) {
$mobileerr = "Please enter a valid 10-digit mobile number";
} else {
$postmobile = true;
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}