Forum Moderators: coopster
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
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:WebmasterWorld breaks pipes so ¦ must be replaced with an unbroken pipe
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.
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.
if (!eregi ("^([a-z]+¦[0-9]+)$", $testval)) error();
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
}