Jago6060 Posted May 5, 2008 Share Posted May 5, 2008 I have a form that need to validate it's fields, and then use the server's cgimail. The problem I'm having is that my formIsValid() function alerts when something is wrong but the script continues to send the fields. I thought that maybe I could set it up to show the submit button once all the required fields were filled. If that's the best way to do this, any help in doing it would be appreicated, otherwise, I'm open to ther solutions. Here is the stuff involved. function formIsValid() { var email = document.inquiry.email.value; var comment = document.inquiry.note.value; var name = document.inquiry.name.value; <!-- code borrowed from phpfreaks member stockton --> var bError = false; var strErrorMsg = ""; if (name == "") { strErrorMsg += "You must enter a name\n"; bError = true; } else if ((email == "") || (email.indexOf(' ')==-1 && 0<email.indexOf('@') && email.indexOf('@')+1 < email.length)) { strErrorMsg += "You must enter a valid email address\n"; bError = true; } else if (comment == "") { strErrorMsg += "Please enter a comment or question\n"; bError = true; } if (bError == true) { alert(strErrorMsg); } } The form... <form name=inquiry action="/cgi-bin/cgiemail" onSubmit = "return formIsValid()" method="post"> <table border = 0 cellpadding = 3> <tr><td align = right> <font face = Comic Sans MS color = #b7b7b7>*Name:</td> <td><input name = name size = 60></td> </tr><tr><td align = right> <font face = Comic Sans MS color = #b7b7b7>*E-mail Address:</td> <td><input name = email size = 60></td> </tr><tr><td align = right> <font face = Comic Sans MS color = #b7b7b7>Phone Number:</td> <td><input name = phone size = 60></td> </tr><tr><td align = right> <font face = Comic Sans MS color = #b7b7b7>*Question/Comment Topic</td> <td> <select name = re> <option>Product Information</option> <option>Installation Question</option> <option>Car Audio</option> <option>Accessories</option> <option>Bedliners</option> <option>Other</option> </select> </td> </tr><tr><td align = right> <font face = Comic Sans MS color = #b7b7b7>*Comments:</td> <td colspan = 2 align = center> <textarea rows = 10 cols = 46 name = note></textarea> <br> <input type = submit value = "Contact Us"> </td></tr></table></form> P.S. I didn't write the form, I'm helping a friend, so please ignore the coding format, thanks. Quote Link to comment Share on other sites More sharing options...
xenophobia Posted May 6, 2008 Share Posted May 6, 2008 Two things need to be done: 1.) Change all the true to false and false to true. So that when ur function will return false when invalid and true if valid. 2.) add this to the end of ur function statement return bError; To return the status. Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted May 6, 2008 Author Share Posted May 6, 2008 Two things need to be done: 1.) Change all the true to false and false to true. So that when ur function will return false when invalid and true if valid. 2.) add this to the end of ur function statement return bError; To return the status. This look better? function formIsValid() { var email = document.inquiry.email.value; var comment = document.inquiry.note.value; var name = document.inquiry.name.value; <!-- code borrowed from phpfreaks member stockton --> var bError = true; var strErrorMsg = ""; if (name == "") { strErrorMsg += "You must enter a name\n"; bError = false; } else if (email == "") { strErrorMsg += "You must enter a valid email address\n"; bError = false; } else if (comment == "") { strErrorMsg += "Please enter a comment or question\n"; bError = false; } if (bError == false) { alert(strErrorMsg); } return bError; } 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.