RE: [PHP] Ereg problem
| From: | Beauford | Date: | Wed, 28 Jun 2006 01:57:31 +0000 |
| Subject: | RE: [PHP] Ereg problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-238756@lists.php.net to get a copy of this message | ||
-----Original Message-----
From: Robert Cummings [mailto:robert@interjinn.com]
Sent: June 27, 2006 12:58 PM
To: Beauford
Cc: PHP-General
Subject: Re: [PHP] Ereg problem
On Tue, 2006-06-27 at 12:14, Beauford wrote:
> One more in my recent woes. The last elseif does not work in the code
> below
> - even if the string is correct it always says it's incorrect. Even if
> I remove everything else and just have the ereg satement is doesn't
> work either.
>
> The code below is in a function and $_POST['password1'] is passed to
> the function.
>
> $field = "password1"; //Use field name for password
>
> if(!$subpass){
> $form->setError($field, " Password not entered");
> }
> elseif(strlen($subpass) < 6) {
> $form->setError($field, " Too Short");
> }
> elseif(strlen($subpass) > 10) {
> $form->setError($field, " Too Long");
> }
> elseif(!ereg('[^A-Za-z0-9]', trim($subpass))) {
> $form->setError($field, " Not alphanumeric");
> }
You're double negating. You check the negative return of ereg() and ereg
checks for the pattern NOT existing.
<?php
elseif( !ereg( '[A-Za-z0-9]', trim( $subpass ) ) )
// the ereg check is still wrong though and should be...
elseif( !ereg( '^[A-Za-z0-9]+$', trim( $subpass ) ) )
// though personally, I'd use the following...
elseif( !ereg( '^[[:alnum:]]+$', trim( $subpass ) ) )
?>
Ahhh, I was thinking of it the other way round. Thanks.