Jump to content

[SOLVED] ereg check for letters- should work? but does not


Recommended Posts

Hello,

 

I am writing an Asset Management DB, tailored for our company. All is going well, however, I am getting stuck on a Simple ereg match.

 

I have a web form that asks the user to enter a "Kit Number". The kit number must be an integer ONLY.

 

the web form includes:

 

  <tr>

  <td>Vehicle Kit Number:</td>

  <td><input type="text" name="inveh_kit_number"></td>

  </tr>

 

the value in "inveh_kit_number" is then passed to a php script, "in_vehicle.php "

 

The relevent code in "in_vehicle.php" to check for any letters is:

 

if (isset($_POST['inveh_kit_number']) || $_POST['inveh_kit_number'] !== "") {

    if (ereg('[^A-Za-z]', $_POST['inveh_kit_number'])) {   

    $errorKitNum = true;

echo "in-vehicle Kit ereg: " . $errorKitNum ;

    }

}

 

 

here, i check to see if something is entered in the $_POST['inveh_kit_number'] field and if it is check to see if there are any letters in it.

If there are any letters the variable $errorKitNum is set to 'true'.

 

However, even when I enter an integer, say '2' (without quotes) it always evaluates as true and matches.

 

That is the outcome on screen is:

               in-vehicle Kit ereg: 1

       i.e. $ errorKitNum = true;

 

 

 

If I remove the if statement including isset and !== (the first if statement) it still evaluates as true.

 

I do not understand why the digit '2' is matched in this ereg if statement.

 

any assistance is welcome!

 

cheers,

DJphp

 

 

That's because of the Not (^) operator you have in the character class.  Also, for regexes, I'd seriously suggest PCRE over POSIX because it's removed in PHP6.  Now, for this, you could also use the function ctype_digit(). >_>

 

if (!ctype_digit($_POST['inveh_kit_numbers'])) {

  $errorKitNum = true;

  //etc

}

That's because of the Not (^) operator you have in the character class. 

 

 

what a silly mistake- i had it backwards. ::) 

i had been staring at that for about 2 hours! >:(

thanks for picking it up!

 

 

Also, for regexes, I'd seriously suggest PCRE over POSIX because it's removed in PHP6.  Now, for this, you could also use the function ctype_digit(). >_>

 

if (!ctype_digit($_POST['inveh_kit_numbers'])) {

   $errorKitNum = true;

   //etc

}

 

i did not know about ctype_digit but it is perfect.

it also saves me writing the regex for non alphanumeric characters- my regex is useless (obviously!).

thanks for the heads up on PHP6.

 

cheers,

DJphp

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.