Hi, I have a landing page that colect name and email. How to do I make the filed mandatory? Would be happy to get your help. Tnx Brooke P.S.: I attached most of the source code on the page. ------------ <body> <script> function valid_required(field) { if(field=="") { return false; } return true; } function valForm() { if(!valid_required(document.myform.email.value)) { alert("Please fill out all the fields") return false; } return true; } </script> <tr> <td> <form method="post" name="myform" onSubmit="return valForm()" action="http://mydomian/index.php"> <p align="right"><font color="darkmagenta"><u><b>Please leave your name and phone number</b></u><br> <b><font color="mediumvioletred">First Name: <input type="text" size="23" name="First_name0"> Phone Number: <input type="text" size="26" name="Phone_Number1"></p> <center> <input type="submit" name="submit" value="More Info" style="font-weight: 700"> </center></div> </form> </font></b></font></td> </tr> </table> </font></b></font> </body>
Do you have a field named "email"? I see you're checking it but I don't see it on the form. I tested it and switched it to "Phone_Number1", which worked. Let me know if that was the problem for you.
any one can disable your validation by just disabling javascript ... since you r using php therefore you can use server side validation ..... i think that will be better for you
WORKED!!! Thank you katendarcy. Webvinay - it depends on your visitors. I don't promote to internet savvy. Brooke
Glad it worked for you. I wasn't sure if you just hadn't posted all of the fields, or if it really was the problem. : ) I'm not sure what else you have planned for the validation, but I was thinking about this after I posted last night and wanted to make a suggestion. If you do have multiple fields on a form, it's often a better experience for the user if you just tell them upfront what they need to fix. (That's my opinion, at least.) That way, you just have one alert for all the errors. If you want to do that, something like this would work: /*NOTE: I use a different method for getting the values of input fields. But, you could still use "document.myform.....". Also, I just used the check you're currently using.*/ function valForm(){ var txtField1; var txtField2; var strError; txtFName = document.getElementById('txtFName'); txtLName = document.getElementById('txtLName'); strError = ""; strErrorMsg = "You must fix the following errors before proceeding:\n"; if (txtField1.value==""){ strError = strError + " - Please enter a first name.\n"; } if (txtField2.value==""){ strError = strError + " - Please enter a last name.\n"; } if (strError.value=="" || strError.length == 0){ //do nothing... } else{ strErrorMsg = strErrorMsg + strError; alert(strErrorMsg); return false; } } Personally I use JS and PHP for validation. The JS to save the server and for those that have it running, and the PHP as a fall back and for validation the client can't do. But, it is up to you. Take care! : )