I have a simple textbox in HTML which takes only integers, they have to be EXACTLY 6 characters. If these conditions arent met, there should be appropriate messages given to the user and the value shouldnt be $_POST'ed.

$idLength = strlen($_POST['customerid']);

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(empty($_POST['customerid']))
    {
        $idErr = " *Enter ID";
    }
else if(($idLength > 0) && ($idLength < 6))
       {
       {$idnErr = " ID must be exactly 6 Digits";}
       if((!is_numeric($_POST['customerid'])))
       {$idnumErr = " ID must only be numerical values";}
       }

else if ((is_numeric($_POST['customerid']))&&($idLength = 6)) 
    {
        $id = $_POST['customerid'];
    }
echo "$id";

So with that code, i check if the input box is empty, if it is, it displays '*Enter ID' next to the box. I check if the value is greater than 0 and less than 6 (maxlength = 6 in html), if it is, it says "ID must be exactly 6 Digits". I also check if the characters are numbers, if they arent, it displays 'ID must only be numerical values'.

If the user enters a vaild ID, only then is the id stored in the variable. For example:- 123456. All this seems to work fine.

My problem occurs when i enter exactly 6 characters that arent meant to be taken such as, 12345a or ABCDE3. The code doesnt store them into $id but it doesnt show an error message like its meant to, i was wondering what i am doing wrong.

I also want to save the input by the user in the textbox if they enter it incorrectly. For example. if the user enters '1234g' in customer id and click submit, i want it to be there in the text box when the page refreshes with the errors.

有帮助吗?

解决方案

Over here:

($idLength = 6)

You are assigning the value 6 to the variable. You should be doing:

($idLength == 6)

== is a comparison operator and checks whether's it's equal to 6.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top