If I do a form input on a site using a text box that I want to be a two-digit integer, how can I check if it is in fact a two digit integer and not something like 'aa' or something of that nature?
Thanks!
When the variable comes in from the form, use the is_int($var) or is_real($var) to check it.
-- Rich Rijnders -- Irvine, CA US
If you want exactly two integers you could do if(is_int($str) && (strlen($str) == 2)) { //process as normal } else { print "Your input must be two integers"; }
hmmm. That's just stupid. Do this:
if(ereg("[0-9]{2}", $str)) print "ok..."; else print "error...";
That looks much better...
Andreas
What, --exactly-- makes Louie's solution "just stupid" and YOURS so much better?
I want to know, and maybe Joey wants to know. Louie might even be curious.
--ph
Calling someone stupid when he helps someone is not nice.
Don't do it.
The solution is not stupid, it's a perfectly working solution. Andreas' solution works about twice as fast as the other solution, that is all.
Nonetheless, despite the bickering, thanks for all your help--all of your solutions worked 🙂
Hey Andreas, if you bothered to notice. Joey said he wanted the field to only allow two integers. Your code allows anything as along as it contains two integers next to each other anywhere in the string. So sd233df would return true. I am sure that wasn't what Joey was looking for. Before you start calling other people stupid, you may want to have a look at yourself. Peace Out! -Louie
$str=5555; if(ereg("[0-9]{1,2}", $str)) print "ok..."; else print "error...";
Why does this result in ok, there are to many numbers!?!
if(ereg("[0-9]{2}$", $str))
should fix it.
-- rad
I believe that still just checks for two consecutive integers. So even if the string is a millino characters long as long as there are two consecutive integers in the string it will return true and in Fredericks case two consecutive integers at the start of the string. Granted Joey can control the number of characters allowed in textbox by ading a size="2" attribute, but this is client side and if he is using GET anyone could change that.
actually, no. the caret ('') and dollar in my regex (but not the original that caused such contention) are assertions which will anchor the substring to the beginning and end of the string.
Good call even STUPID people like me(according to Andreas) can learning something new.