Jump to content

Recommended Posts

I am trying to check a string for numbers

 

 

 

This is what I have:

 


$string="password1";

if(!ereg('[^0-9]', $string))
{
$alert="<script>alert('Your Password must contain At Least 1 Number')</script>";
}

 

It doesn't work...any thoughts?

 

Ultimately the password must contain 8 charactors 1 number and 1 letter

 

 

Link to comment
https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/
Share on other sites

/[(^0-9)]/ thank you for the reply but it didn't work (probably my fault)

 

$string="12345678";

 

Here is all of it:

 


elseif(strlen($string) < 
{
$alert="<script>alert('Your Password must contain At Least 8 Characters')</script>";
} 

elseif(!ereg('[^A-Za-z]', $string]))
{
$alert="<script>alert('Your Password must contain At Least 1 Letter')</script>";
}		

elseif(!ereg('/[(^0-9)]/', $string))
{
$alert="<script>alert('Your Password must contain At Least 1 Number')</script>";
}	

 

I am sure there is one string for all of this but I cant figure it...

the problem with your OP code:

 

if(!ereg('[^0-9]', $string))

 

is that it's a "double negative".  You have a negative char class looking for anything NOT 0-9.  So ereg will return true if there are no numbers in it.  But then you turn around and have a ! in front of it.  So basically overall the condition will evaluate true if there IS a number in the string. 

 

But anyways, as someone else pointed out, ereg is deprecated.

Hmm.. topic is flaged as solved.. I was going to ask, can the password contain any mix of any 8 characters (aside from requiring 1 letter and 1 number)? In otherwords, would these samples be legal passwords?

 

..?@!y.6

uÊ7³mÖx¹

 

but I guess it doesn't matter now  :P

(Tangent/)Offtopic: @Crayon Violent: It is a double negative, but your explanation is not correct. In the code described, ereg will return 1 (i.e, true/found) if there is any single non-digit character. Rather than returning true "if there are no numbers in it", it will return true if there is one (or more) non-digit in the string. The "logical not" operator (!) is then used to flip the true/false so the full condition of the if statement will be true if there is not at least one non-digit in the string (ie, true if the value only contains digits). In other words true for 1234567 and false for 12345a.

(Tangent/)Offtopic: @Crayon Violent: It is a double negative, but your explanation is not correct. In the code described, ereg will return 1 (i.e, true/found) if there is any single non-digit character. Rather than returning true "if there are no numbers in it", it will return true if there is one (or more) non-digit in the string. The "logical not" operator (!) is then used to flip the true/false so the full condition of the if statement will be true if there is not at least one non-digit in the string (ie, true if the value only contains digits). In other words true for 1234567 and false for 12345a.

 

yeah you're right. that's how I understood it in my head; not very good at translating that to paper though :(

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.