2

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;
}
2
  • Please check any other javascript error using firebug. If you encounter any java script error while loading your page, it will not work as expected. Commented Dec 21, 2015 at 8:19
  • Thank you for replying. I have not included any javascript whatsoever. The page contains HTML, CSS and PHP. However, I have noticed that if I remove the code and type it in again, it starts working. This has happened twice until now. Commented Dec 21, 2015 at 9:02

2 Answers 2

3

How about change this code:

if (preg_match('/[^0-9]{10}/', $mobile)) {
        $mobileerr = "Please enter a valid 10-digit mobile number";
    }

with this one;

if(strlen($mobile) != 10 || $mobile != ctype_digit($mobile)) {
$mobilerr = "Please..";
}

It will check if user input 10 character long and if it's contains only numbers.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the tip, LetsSeao, this didn't work. I entered a full 10-digit number and it still gave an error asking for a valid entry to be made.
UPDATE: This worked. I made a small mistake which caused it to fail. Now I have to check if it continues working without failing like earlier. Thank you, LetsSeo!
I'm happy to be helpful. You can accept my answer if you like :)
Accepted! I hope it works consistently. Thank you! :)
0

initilaize

$postmobile = false;

at beginning and then at end check if it is true of false.

Your modified version of code could be

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

$postmobile = false;
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;
    }
}

if ($postmobile === false) {
    die("Invalid Mobile Number"); //Here you can postback error message and skip further execution.
}

1 Comment

I had already set $postmobile to false. I'm sorry I didn't mention it. But the problem is not with $postmobile. The validation itself is not returning the required result and I'm not able to figure out why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.