Jump to content

[SOLVED] Check for numbers


phpretard

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 :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.