PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hi, I'm trying to make sure a user can only put in 1 - 12 for a month field.
Here's the code I'm trying:
//policy_eff_date_mo Policy Start Date Month
$policy_eff_date_mo = settype($policy_eff_date_mo, int) + 1;
if($policy_eff_date_mo == '' || ($policy_eff_date_mo >= 1 AND $policy_eff_date_mo <= 12)) {
$error_msg.="Please enter a Policy Start Date month. (i.e. Feb = 2 or Dec = 12)<br>";
}
When I enter 22 the validation doesn't work(which it shouldn't) but when I enter 8 or 08 I get back 2 and still get the error message. What am I doing wrong??
settype() doesn't return the new value. It returns true on success, false on failure. The new value is the variable you passed in itself. That explains the results you are getting.
Now, you have a logic error. You are checking if the value is between 1 and 12, inclusively. If it is, you give the error. You need to invert the expression: