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!
I am trying to validate a year that a user inputs from a form. The format MUST be in "2007" format so I came up with the code here. But it is not working.
//Check experation date
if ("$date_override" == '1')
{
if(trim("$pn_duesexdate") =='' || strlen(trim("$pn_duesexdate")) == 4)
{
$error_msg.="<li>The experation year is invalid.</li>";
$error_12 = ' class="formerror"';
}else{
// now check if year is a valid format
if(!ereg("([0-9]{4})", "$pn_duesexdate"))
{
$error_msg.="<li>The experation year is invalid.</li>";
$error_12 = ' class="formerror"';
}
}
}
If it is coming from a form you could simple use a select box that has the dates predefined within it. This way the data you know will be a certain format, and you can always check to see if it is not.
// now check if year is a valid format
if(!ereg("([0-9]{4})", "$pn_duesexdate"))
{
$error_msg.="<li>The experation year is invalid.</li>";
$error_12 = ' class="formerror"';
}
I've already given two alternatives to ereg(), which is slower than both alternatives as well as (if memory serves) will be deprecated in the next major php version anyways..
Now that I am looking at your code again, try re-arranging your logic flow.