DJphp Posted September 3, 2008 Share Posted September 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 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 } Quote Link to comment Share on other sites More sharing options...
DJphp Posted September 3, 2008 Author Share Posted September 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.