darko Posted February 11, 2006 Share Posted February 11, 2006 hey guys i need a a script that validates forms i'm using the following script[code]<script language="javascript">function form_Validator(form){ if (form.name.value == "") { alert("Please enter your Full Name."); form.name.focus(); return (false); } if (form.email.value == "") { alert("Please enter your email."); form.email.focus(); return (false); } if (form.comments.value == "") { alert("Please enter your query."); form.comments.focus(); return (false); } return (true); } //--> </script> <form action="mail.php" method="post" onSubmit="return form_Validator(this)"> <div align="center"><span class="style5">Your Full Name:<br><br></span></div> <center><input name="name" type="text" size="40" maxlength="40"> </center><div align="center"><br> <span class="style5">Your E-mail:<br><br></span></div><center> <input name = "email" type="text" size="40" maxlength="40"></center><div align="center"><br> <span class="style5">Please specify a Subject:</span><br> <br></div><center><select name="subject"> <option>Ick an option</option> <option>Food</option> <option>Cars</option> <option>Other</option></select></center><div align="center"><br><br> <span class="style5">Comments:</span><br> <textarea name="comments" cols="30" rows="8"></textarea> <br> <br> <input type="submit" value="Submit"> <input type="reset" value="Reset"></div></form>[/code]----------------------------------------------------------------------------------------the problem is i don't know how validate if the user has specified one of the options. and lets say i had a radion button, i want to disable the submit button 'till the user clciks on one of the radion button, (like for example he has to choose the I Agree button for a TOS)---i hope that makes sense, and please help meThanksDarko Quote Link to comment https://forums.phpfreaks.com/topic/3389-form-validation-htmljavascript/ Share on other sites More sharing options...
Javizy Posted February 12, 2006 Share Posted February 12, 2006 [!--quoteo(post=344805:date=Feb 11 2006, 10:09 AM:name=darko)--][div class=\'quotetop\']QUOTE(darko @ Feb 11 2006, 10:09 AM) [snapback]344805[/snapback][/div][div class=\'quotemain\'][!--quotec--]hey guys i need a a script that validates forms i'm using the following script[code]<script language="javascript">function form_Validator(form){ if (form.name.value == "") { alert("Please enter your Full Name."); form.name.focus(); return (false); } if (form.email.value == "") { alert("Please enter your email."); form.email.focus(); return (false); } if (form.comments.value == "") { alert("Please enter your query."); form.comments.focus(); return (false); } return (true); } //--> </script> <form action="mail.php" method="post" onSubmit="return form_Validator(this)"> <div align="center"><span class="style5">Your Full Name:<br><br></span></div> <center><input name="name" type="text" size="40" maxlength="40"> </center><div align="center"><br> <span class="style5">Your E-mail:<br><br></span></div><center> <input name = "email" type="text" size="40" maxlength="40"></center><div align="center"><br> <span class="style5">Please specify a Subject:</span><br> <br></div><center><select name="subject"> <option>Ick an option</option> <option>Food</option> <option>Cars</option> <option>Other</option></select></center><div align="center"><br><br> <span class="style5">Comments:</span><br> <textarea name="comments" cols="30" rows="8"></textarea> <br> <br> <input type="submit" value="Submit"> <input type="reset" value="Reset"></div></form>[/code]----------------------------------------------------------------------------------------the problem is i don't know how validate if the user has specified one of the options. and lets say i had a radion button, i want to disable the submit button 'till the user clciks on one of the radion button, (like for example he has to choose the I Agree button for a TOS)---i hope that makes sense, and please help meThanksDarko[/quote]Give your submit button an id, say id="submit". Create a function that simply gets submit and does submit.disabled = "true", and call it from <body onLoad="myDisableFunction();">. This will ensure that users who don't have javascript will still be able to use your form.Then, create another function called enableSubmit or something, which calls your validator method, and sets submit.disabled to the value it returns. Call this function onChange for all of your form elements, and when the validation is passed, the submit button will be enabled.Hope this helps. I might use this idea for my forum submits actually :-) Quote Link to comment https://forums.phpfreaks.com/topic/3389-form-validation-htmljavascript/#findComment-11608 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.