Reaper0167 Posted June 11, 2009 Share Posted June 11, 2009 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> Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 11, 2009 Share Posted June 11, 2009 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> Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted June 11, 2009 Author Share Posted June 11, 2009 thanks,,, any ideas about the checkbox validation part of the script Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 11, 2009 Share Posted June 11, 2009 <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?? Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted June 11, 2009 Author Share Posted June 11, 2009 onSubmit="return validate(this); Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted June 11, 2009 Author Share Posted June 11, 2009 Hey,, that worked,, I could of sworn I tried that... Thanks bud. Quote Link to comment 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.