Forum Moderators: coopster

Message Too Old, No Replies

Error Checking in PHP

Error Checking in PHP

         

Imy_S3

2:34 am on Mar 10, 2004 (gmt 0)

10+ Year Member



Hi

I have code below

If User has entered characters instead of nuumbers and then will cause an error
if(ereg("^[a-zA-Z]$*", $_POST['option']))

If text field empty then error
If(empty($_POST['option']))

dnt not how to say if user enters text AND numbers then error. as this particular field requires text only or numbers only but not both
so cannot have 34FG

Hope you can help
Thanks in advance

jonknee

4:57 am on Mar 10, 2004 (gmt 0)

10+ Year Member



I would use is_numeric() [us2.php.net] instead of that crazy regex deal :).

coopster

1:55 pm on Mar 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Good idea, jonknee, but is_numeric [php.net] is going to return
FALSE
if the user entered all alphabetic characters. Imy_S3 says that all alphabetic characters is OK. Therefore, I think the quickest way is going to be a regular expression:
if (preg_match("/\d+\s*[[:alpha:]]¦[[:alpha:]]\s*\d+/", $_POST['option'])) { 
// there is an error
}
This statement says: 
if there are 1 or more digits (\d+),
followed by 0 or more space characters (\s*),
followed by an alphabetic character ([[:alpha:]]),
OR [¦],
if there is an alphabetic character ([[:alpha:]]),
followed by 0 or more space characters (\s*),
followed by 1 or more digits (\d+),
the statement is true,
meaning there is a mixture of numbers and alphabetic characters,
so give an error message.
WebmasterWorld breaks pipes so ¦ must be replaced with an unbroken pipe

By the way, Imy_S3, if the user enters a zero (0) for the option and zero is indeed a valid entry, empty [php.net] is not going to work for you. You want to check if the variable isset [php.net], then do some comparisons.

dcrombie

5:21 pm on Mar 10, 2004 (gmt 0)



Can't you do something like this?

if (!eregi ("^([a-z]+¦[0-9]+)$", $testval)) error();

coopster

6:28 pm on Mar 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Absolutely. Much easier. It must begin, end and contain only alphabetic characters or it must begin, end and contain only numbers. That's it.

My original thought process was as yours, dcrombie, to negate the conditions and then OR the classes, but for some reason spaces kept popping into my head. Like, what if the user keys spaces in there somewhere...I must of been out of it this morning.

I would still recommend using preg_match [php.net] as opposed to eregi [php.net] as the developers state that it is a faster alternative [php.net]. I have never tested this to confirm, but hey, they wrote the code, they can't be wrong...right?

if (!preg_match("/^([a-z]+¦[0-9]+)$/i", $_POST['option'])) {  
// there is an error
}

Netizen

6:31 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



On some code I wrote about 3 years ago preg was about 5-10 times faster than ereg. FYI.

coopster

6:39 pm on Mar 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Cool. Thanks.