PRodgers4284 Posted February 6, 2008 Share Posted February 6, 2008 I need some validation to check if i checkbox has been ticked on a website registration form, if its not ticked output an error, can anyone help me with this, my validation error checking code so far is: //Username check) if (empty($username)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a username $username_message = '*Please enter a username*'; } if(usernameTaken($username,$conn)) { $error_stat = 1; $username_message = '*User name is taken please choose another one*'; } $username = $_POST['username']; $username = trim($username); if (strlen($username) > 12){ $error_stat = 1; $username_message = '*The username must be 12 characters or less*'; } //Password check) if($password1 != $password2) { $error_stat = 1; $password_message = '*Passwords don\'t match*'; } if (empty($password1)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a username $password_message = '*Please enter a password*'; } if(!$password1 || !$password2) { $error_stat = 1; $password_message = '*Please enter both passwords*'; } //Forename check) if (empty($forename)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a username $forename_message = '*Please enter your forename*'; } else if (ctype_digit($forename)) { $error_stat = 1; $forename_message .= '*Invalid forename*'; } $forename = $_POST['forename']; $forename = trim($forename); if (strlen($forename) > 12){ $error_stat = 1; $forename_message = '*The forename must be 12 characters or less*'; } //Surname check) if (empty($surname)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a username $surname_message = '*Please enter your surname*'; } else if (ctype_digit($surname)) { $error_stat = 1; $surname_message .= '*Invalid surname*'; } $surname = $_POST['surname']; $surname = trim($surname); if (strlen($surname) > 12){ $error_stat = 1; $surname_message = '*The surname must be 12 characters or less*'; } //Email check) if (empty($email)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter an email address $email_message = '*Please enter your email address*'; } //Check format of email address entered else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)){ $error_stat = 1; //Set the message to tell the user to enter a valid email address $email_message = '*Invalid Email Address*'; } if(emailTaken($email,$conn)) { $error_stat = 1; $email_message = '*Email is taken please choose another one*'; } $email = $_POST['email']; $email = trim($email); if (strlen($email) > 30){ $error_stat = 1; $email_message = '*The email address must be 30 characters or less*'; } //Mobile number check) if (empty($mobile)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a dob $mobile_message = '*Please enter your mobile number*'; } else if (!ctype_digit($mobile)) { $error_stat = 1; $mobile_message .= '*The mobile phone number must be only numbers*'; } if(mobileTaken($mobile,$conn)) { $error_stat = 1; $mobile_message = '*Mobile already in use, choose another one*'; } $mobile = $_POST['mobile']; $mobile = trim($mobile); if (strlen($mobile) > 11){ $error_stat = 1; $mobile_message = '*Invalid mobile number*'; } Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/ Share on other sites More sharing options...
revraz Posted February 6, 2008 Share Posted February 6, 2008 Do the same as the rest except use the checkbox name. Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459845 Share on other sites More sharing options...
thebadbad Posted February 6, 2008 Share Posted February 6, 2008 HTML: <input type="checkbox" name="checkthis" /> PHP: <?php if($_POST['checkthis']) { //it's checked; do something } else { //it's not checked, echo error message } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459847 Share on other sites More sharing options...
PRodgers4284 Posted February 6, 2008 Author Share Posted February 6, 2008 HTML: <input type="checkbox" name="checkthis" /> PHP: <?php if($_POST['checkthis']) { //it's checked; do something } else { //it's not checked, echo error message } ?> how could i check it if the checkbox is equal to "false" then output the error message? Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459879 Share on other sites More sharing options...
rhodesa Posted February 6, 2008 Share Posted February 6, 2008 What do you mean by "equal to false"? Not checked? If the checkbox isn't checked, it's not sent to the $_POST variable. So you shouldn't need to check it's value, rather just if it's set. Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459886 Share on other sites More sharing options...
PRodgers4284 Posted February 6, 2008 Author Share Posted February 6, 2008 What do you mean by "equal to false"? Not checked? If the checkbox isn't checked, it's not sent to the $_POST variable. So you shouldn't need to check it's value, rather just if it's set. If the checkbox is not checked when the submit button is clicked i would like an error to appear, im not posting the value of the check box to the database, i have terms and conditions option on my register form and i want the some validation to stop the form from being submitted if the checkbox is not checked. Phil Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459925 Share on other sites More sharing options...
laffin Posted February 6, 2008 Share Posted February 6, 2008 if(!isset($_POST['mycheckbox'])) die("Checkbox not checked"); if mucheckbox is not checked, than it wont be in the $_POST array. so u check if it exists or not using isset function Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459928 Share on other sites More sharing options...
rhodesa Posted February 6, 2008 Share Posted February 6, 2008 That is a good server side check (which you definitely should have). But I would also add this to your form: <input type="checkbox" name="terms" onclick="document.getElementById('formSubmit').disabled = (!this.checked);" /> Read Terms & Conditions<br /> <input type="submit" id="formSubmit" disabled=true /> Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459931 Share on other sites More sharing options...
PRodgers4284 Posted February 6, 2008 Author Share Posted February 6, 2008 if(!isset($_POST['mycheckbox'])) die("Checkbox not checked"); if mucheckbox is not checked, than it wont be in the $_POST array. so u check if it exists or not using isset function I got it working, sorry i dont what i was doin there Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459932 Share on other sites More sharing options...
PRodgers4284 Posted February 6, 2008 Author Share Posted February 6, 2008 That is a good server side check (which you definitely should have). But I would also add this to your form: <input type="checkbox" name="terms" onclick="document.getElementById('formSubmit').disabled = (!this.checked);" /> Read Terms & Conditions<br /> <input type="submit" id="formSubmit" disabled=true /> I got that working, im sorry i dont know what i was thinkin there, thanks for you help, really appreciate it. Phil Quote Link to comment https://forums.phpfreaks.com/topic/89737-checkbox-validation-help/#findComment-459934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.