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!
Using the below field validation code to require number entry; however, the code consider "0" and invalid entry. What would I need to tweak to accept "0" as a opposed to NULL (no field entry)?
if(!preg_match("/^[0-9]{1,5}$/", $_POST['sugar']) && $_POST['sugar']) {
$errors[] = "Yes";
$sugarentryerror = "\t<br><span class=pred>Invalid value entered. Must me a numeric value less than 5 digits or leave blank.</span>\n";
}
if(!preg_match("/^[0-9][color=#FF0000]{0,5}[/color]$/", $_POST['sugar'])) {
$errors[] = "Yes";
$sugarentryerror = "\t<br><span class=pred>Invalid value entered. Must me a numeric value less than 5 digits or leave blank.</span>\n";
}
Still seems to be blocking "0"
Last edited by edawson003 on Thu Sep 17, 2009 12:34 am, edited 2 times in total.
if(!$_POST['servingsize']){
$reqerrors[] = "Yes";
$servingsizeerror = "\t<br><span class=pred>You must enter amount of servings.</span>\n";
}
elseif(!preg_match("/^[0-9]{1,2}$/", $_POST['servingsize']) && $_POST['servingsize']) {
$reqverrors[] = "Yes";
$servingsizeerror = "\t<br><span class=pred>Invalid value entered. Must be a numeric value less than 2 digits.</span>\n";
}
Last edited by edawson003 on Fri Sep 18, 2009 4:23 am, edited 1 time in total.
if (preg_match("/^-?([0-9])+\.?([0-9])+$/", $string))
1. Looks for an optional minus sign at the beginning of the string (^-?)
2. Looks for one or more number (([0-9])+)
3. Looks for an optional decimal point (\.?)
4. Looks for one or more numbers at the end of the string (([0-9])+$)