Jump to content

checkbox validation help


PRodgers4284

Recommended Posts

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*'; 
}  
  

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.