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

Link to comment
Share on other sites

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

 

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.