Captain Salad Posted March 27, 2006 Share Posted March 27, 2006 Sorry to bug this forum with yet more of my woes but for some reason when I test my form, if I add a 0 (just a zero, nothing more) to a required field it doesnt pick it up and states that I missed a required field.Any help on this one? Thanks you for any time and help, God bless Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/ Share on other sites More sharing options...
annihilate Posted March 27, 2006 Share Posted March 27, 2006 [a href=\"http://uk2.php.net/empty\" target=\"_blank\"]http://uk2.php.net/empty[/a]The string '0' is considered empty, so will have to use a different method of validation if you want to allow zeros. Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21208 Share on other sites More sharing options...
Guest footballkid4 Posted March 27, 2006 Share Posted March 27, 2006 You can use:[code]if ($_REQUEST['email'] == ""){ //not supplied}[/code]Or[code]if ( strlen( trim( $_REQUEST['email'] ) ) > 0 ){ //supplied}[/code] Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21211 Share on other sites More sharing options...
Captain Salad Posted March 27, 2006 Author Share Posted March 27, 2006 [!--quoteo(post=358901:date=Mar 27 2006, 09:35 AM:name=annihilate)--][div class=\'quotetop\']QUOTE(annihilate @ Mar 27 2006, 09:35 AM) [snapback]358901[/snapback][/div][div class=\'quotemain\'][!--quotec--][a href=\"http://uk2.php.net/empty\" target=\"_blank\"]http://uk2.php.net/empty[/a]The string '0' is considered empty, so will have to use a different method of validation if you want to allow zeros.[/quote]Thanks for your input, I was afraid of that. Any help with the code I would need to add in order to allow 0? I am a total newb to PHP and coding of any sort so any further help will be greatly appreciated.God bless Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21212 Share on other sites More sharing options...
Captain Salad Posted March 27, 2006 Author Share Posted March 27, 2006 [!--quoteo(post=358904:date=Mar 27 2006, 09:42 AM:name=footballkid4)--][div class=\'quotetop\']QUOTE(footballkid4 @ Mar 27 2006, 09:42 AM) [snapback]358904[/snapback][/div][div class=\'quotemain\'][!--quotec--]You can use:[code]if ($_REQUEST['email'] == ""){ //not supplied}[/code]Or[code]if ( strlen( trim( $_REQUEST['email'] ) ) > 0 ){ //supplied}[/code][/quote]Hi, Thank you for trying to help me, so would this be correct? $yearsataddress = if ($_REQUEST['yearsataddress'] == ""){ //not supplied} $monthsataddress = $_REQUEST['monthsataddress'] ; Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21216 Share on other sites More sharing options...
annihilate Posted March 27, 2006 Share Posted March 27, 2006 Try this, substitute the function empty in your script for checkLengthExample use: checkLength($title, 0, 50);[code]// String length// Usage: checkLength(string to check length of, min length, max length)function checkLength($string, $min, $max) {$length = strlen(trim($string)); if ( ($length < $min) || ($length > $max) ) { return FALSE; } else { return TRUE; }}[/code] Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21218 Share on other sites More sharing options...
Captain Salad Posted March 27, 2006 Author Share Posted March 27, 2006 Hi, thanks for you help, sadly I think I am way over my head as I cant get it to work still and not sure wher I should add the code (as I said I am total noob) , is there any chance you could be so kind as to amend the below code so that the $yearsataddress field proceses 0 if entered?Sorry for all these pleas for help.God bless Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21238 Share on other sites More sharing options...
annihilate Posted March 27, 2006 Share Posted March 27, 2006 Put this anywhere on the page, I would put at the top though.[code]// String length// Usage: checkLength(string to check length of, min length, max length)function checkLength($string, $min, $max) {$length = strlen(trim($string)); if ( ($length < $min) || ($length > $max) ) { return FALSE; } else { return TRUE; }}[/code]Then change this code section:[code]if (!isset($_REQUEST['email'])) {header( "Location: http://www.brittalk.com/brittalk/Templates...actUs.htm" );}elseif (empty($email) || empty($transfer) || empty($title) || empty($firstname) || empty($lastname) || empty($dob) || empty($currentaddress)[/code]To this (I would run the checkLength function on all fields)[code]if (!isset($_REQUEST['email'])) {header( "Location: http://www.brittalk.com/brittalk/Templates...actUs.htm" );}elseif (checkLength($email,1,40) || checkLength($transfer,1,50) )// etc etc etc[/code]If you just want it on the yearsataddress field, then change empty($yearsataddress) to checkLength($yearsataddress, 1, 5) or something. The 1 is the min number of digits, and the 5 is the max. Just change those numbers depending on how long or short the field can be. Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21246 Share on other sites More sharing options...
Captain Salad Posted March 28, 2006 Author Share Posted March 28, 2006 Hi, I changed the code to what you suggested but I get an "error missed a field" message for no matter what I type in the fields, have I missed something, see edited code below.god bless<?function checkLength($string, $min, $max) {$length = strlen(trim($string)); if ( ($length < $min) || ($length > $max) ) { return FALSE; } else { return TRUE; }} $transfer = $_REQUEST['transfer'] ; $email = $_REQUEST['email'] ; $existingnumber = $_REQUEST['existingnumber'] ; $pacnumber = $_REQUEST['pacnumber'] ; $title = $_REQUEST['title'] ; $firstname = $_REQUEST['firstname'] ; $middleinitial = $_REQUEST['middleinitial'] ; $lastname = $_REQUEST['lastname'] ; $dob = $_REQUEST['dob'] ; $currentaddress = $_REQUEST['currentaddress'] ; $town = $_REQUEST['town'] ; $postcode = $_REQUEST['postcode'] ; $residentialstatus = $_REQUEST['residentialstatus'] ; $yearsataddress = $_REQUEST['yearsataddress'] ; $monthsataddress = $_REQUEST['monthsataddress'] ; $previousaddress = $_REQUEST['previousaddress'] ; $previoustowm = $_REQUEST['previoustown'] ; $previouspostcode = $_REQUEST['previouspostcode'] ; $hometel = $_REQUEST['hometel'] ; $daytimetel = $_REQUEST['daytimetel'] ; $bankaccountyears = $_REQUEST['bankaccountyears'] ; $bankaccountmonths = $_REQUEST['bankaccountmonths'] ; $occupationalstatus = $_REQUEST['occupationalstatus'] ; $creditcard = isset($_POST['creditcard']) ? $_POST['creditcard'] : 0; $yearsinjob = $_REQUEST['yearsinjob'] ; $monthssinjob = $_REQUEST['monthsinjob'] ; $accountholder = $_REQUEST['accountholder'] ; $accountnumber = $_REQUEST['accountnumber'] ; $branchsortcode = $_REQUEST['branchsortcode'] ; $printnamebank = $_REQUEST['printnamebank'] ; $datebank = $_REQUEST['datebank'] ; $termsconditions = $_REQUEST['termsconditions'] ; $printnameterms = $_REQUEST['printnameterms'] ; $printdateterms = $_REQUEST['printdateterms'] ; if (!isset($_REQUEST['email'])) { header( "Location: [a href=\"http://www.brittalk.com/brittalk/Templates/contactUs.htm"\" target=\"_blank\"]http://www.brittalk.com/brittalk/Templates...actUs.htm"[/a] ); } elseif (checkLength($email,1,50) || checkLength($transfer,1,50) || checkLength($title,1,50) || checkLength($firstname,1,50) || checkLength($lastname,1,50) || checkLength($dob,1,50) || checkLength($currentaddress,1,50) || checkLength($town,1,50) || checkLength($postcode,1,50) || checkLength($residentialstatus,1,50) || checkLength($yearsataddress,1,50) || checkLength($monthsataddress,1,50) || checkLength($hometel,1,50) || checkLength($daytimetel,1,50) || checkLength($bankaccountyears,1,50) || checkLength($bankaccountmonths,1,50) || checkLength($occupationalstatus,1,50) || checkLength($monthssinjob,1,50) || checkLength($yearsinjob,1,50) || checkLength($accountholder,1,50) || checkLength($accountnumber,1,50) || checkLength($branchsortcode,1,50) || checkLength($printnamebank,1,50) || checkLength($datebank,1,50) || checkLength($termsconditions,1,50) || checkLength($printnameterms,1,50) || checkLength($printdateterms,1,50)) { Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21500 Share on other sites More sharing options...
Captain Salad Posted March 28, 2006 Author Share Posted March 28, 2006 sorry double post Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21509 Share on other sites More sharing options...
annihilate Posted March 28, 2006 Share Posted March 28, 2006 I would change all the || into &&. And if you want to allow some fileds to be left blank, put the min value as 0. I have quickly done a test of whether it works and it works perfectly. This is the little bit of code I tested on.[code]<?phpfunction checkLength($string, $min, $max) {$length = strlen(trim($string)); if ( ($length < $min) || ($length > $max) ) { return FALSE; } else { return TRUE; }}$name = trim(strip_tags($_POST['Name'])); // Name$email = trim($_POST['Email']); // Email if ( checkLength($email, 1, 50) && checkLength($name, 1, 50)){echo 'Passed validation';}else{echo 'Failed';}?> <form name="Contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table class="padform"> <tr><td><label for="Name">Name</label></td><td><input name="Name" id="Name" type="text" size="50" maxlength="50" value="<?php if ( isset($_POST['Name']) ) echo trim(strip_tags($_POST['Name'])); ?>" /></td></tr> <tr><td><label for="Email">Email</label></td><td><input name="Email" id="Email" type="text" size="50" maxlength="50" value="<?php if ( isset($_POST['Email']) ) echo trim($_POST['Email']); ?>" /></td></tr> <tr><td> </td><td align="center"><input name="Submit" type="submit" value="Contact" /></td></tr> </table> </form>[/code] Link to comment https://forums.phpfreaks.com/topic/5930-0-not-recognised-as-php-required-field-imput/#findComment-21536 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.