Jump to content

[SOLVED] checkbox validation help


Reaper0167

Recommended Posts

my text field validation works,,  but check out the checkbox validation....What am I doing wrong?

<script type="text/javascript" language="javascript">
    function validate(form2)
    {
    var valid = true;
<!--******************************************************************************************************************-->
    if (!form2.username.value || !form2.username_conf.value)
    {
        document.getElementById('username_error').innerHTML = 'You must enter a username and confirm it.';
        valid = false;
    }
    else if (form2.username.value != form2.username_conf.value)
    {
        document.getElementById('username_error').innerHTML = 'Usernames do not match.';
        valid = false;
    }
    else
    {
        document.getElementById('username_error').innerHTML = '';
    }
    
<!--***************************************************************************************************************-->	
if (!form2.password.value || !form2.password_conf.value)
    {
        document.getElementById('password_error').innerHTML = 'You must enter a password and confirm it.';
        valid = false;
    }
    else if (form2.password.value != form2.password_conf.value)
    {
        document.getElementById('password_error').innerHTML = 'Passwords do not match.';
        valid = false;
    }
    else
    {
        document.getElementById('password_error').innerHTML = '';
    }

<!--***************************************************************************************************************-->		

if (!form2.email.value || !form2.email_conf.value)
    {
        document.getElementById('email_error').innerHTML = 'You must enter a valid email and confirm it.';
        valid = false;
    }
    else if (form2.email.value != form2.email_conf.value)
    {
        document.getElementById('email_error').innerHTML = 'Emails do not match.';
        valid = false;
    }
    else
    {
        document.getElementById('email_error').innerHTML = '';
    }
<!--****************************************************************************************************************-->
if (!form2.agree.checked)
    {
        document.getElementById('agree_error').innerHTML = 'You must agree to the terms.';
        valid = false;
    }
    else
    {
        document.getElementById('email_error').innerHTML = '';
    }

return valid;
    }
</script>

Link to comment
https://forums.phpfreaks.com/topic/161759-solved-checkbox-validation-help/
Share on other sites

The comment style you have used within your javascript code is HTML style comments, for js style comments use the same style as C/PHP

 


//single line comment, spans the duration of the line...
NOTE: This is not commented...

/*
Multiple line comment...
Still comment
*/

Not commented

 

Also, the HTML stle comments can be used to ignore js in old browsers or visitors with js disabled (which includes search engine crawlers) like so:

 

<script type="text/javascript">
<!--
/*
@Inside of the script tags the comments will be ignored by visitors with js enabled and will comment out the js code otherwise. 
*/

// js code etc...

-->
</script>

<script type="text/javascript" language="javascript">
<!--
function validate(form2)
{
    var valid = true;

/***************************************************************************************************************/ 
    if (!form2.username.value || !form2.username_conf.value)
    {
        document.getElementById('username_error').innerHTML = 'You must enter a username and confirm it.';
        valid = false;
    }
    else if (form2.username.value != form2.username_conf.value)
    {
        document.getElementById('username_error').innerHTML = 'Usernames do not match.';
        valid = false;
    }
    else
    {
        document.getElementById('username_error').innerHTML = '';
    }
    
/***************************************************************************************************************/ 
   if (!form2.password.value || !form2.password_conf.value)
    {
        document.getElementById('password_error').innerHTML = 'You must enter a password and confirm it.';
        valid = false;
    }
    else if (form2.password.value != form2.password_conf.value)
    {
        document.getElementById('password_error').innerHTML = 'Passwords do not match.';
        valid = false;
    }
    else
    {
        document.getElementById('password_error').innerHTML = '';
    }
      
/***************************************************************************************************************/    
   
   if ( !form2.email.value || !form2.email_conf.value)
    {
        document.getElementById('email_error').innerHTML = 'You must enter a valid email and confirm it.';
        valid = false;
    }
    else if (form2.email.value != form2.email_conf.value)
    {
        document.getElementById('email_error').innerHTML = 'Emails do not match.';
        valid = false;
    }
    else
    {
        document.getElementById('email_error').innerHTML = '';
    }
/***************************************************************************************************************/ 
   if ( form2.agree.checked == false) // try this?
    {
        document.getElementById('agree_error').innerHTML = 'You must agree to the terms.';
        valid = false;
    }
    else
    {
        document.getElementById('email_error').innerHTML = '';
    }
   
   return valid;
}
--> 
</script>

 

Edit your code to that so the comments work correctly, also, how are you calling the function?

 

<form name="form2" method="post" onsubmit="validate(this)">

 

Something like that??

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.