Sigal Posted November 14, 2007 Share Posted November 14, 2007 Hi,I don't know Javascript and found this tutorial here.But I can't find the way to check something in my site.I want the user the fill in one of three fields so my check should be if phone and fax and email were not filled give a message that one of them should be filled.How do you do that?Thank you,Sigal :) Link to comment Share on other sites More sharing options...
Ingolme Posted November 14, 2007 Share Posted November 14, 2007 First of all, we need to reference these fields:<form action="something.php" method="post" id="form" /><input type="text" id="phone" /><input type="text" id="fax" /><input type="text" id="email" />Then we can have a function that activates when you click a button:<input type="button" value="Submit" onclick="formValidate()" /></form>Put this within the <head> tags of the document:<script type="text/javascript">if(document.getElementById("phone").value.length == 0 && document.getElementById("fax").value.length == 0 && document.getElementById("email").value.length == 0) {alert("You must fill in at least one of the fields");} else {document.getElementById("form").submit();}</script> Link to comment Share on other sites More sharing options...
Synook Posted November 14, 2007 Share Posted November 14, 2007 Uh... the validation needs to be in a function <script type="text/javascript">function formValidate() {if(document.getElementById("phone").value.length == 0 && document.getElementById("fax").value.length == 0 && document.getElementById("email").value.length == 0) {alert("You must fill in at least one of the fields");} else {document.getElementById("form").submit();}}</script> However, it is better to use the form's onsubmit event handler because if done Ingolme's way if the user presses enter on one of the fields the form will be submitted without validation. The input code remains the same, for the form tag have <form action="something.php" method="post" id="form" onsubmit="return formValidate()" /> And in the document head <script type="text/javascript">function formValidate() {if(document.getElementById("phone").value.length == 0 && document.getElementById("fax").value.length == 0 && document.getElementById("email").value.length == 0) {alert("You must fill in at least one of the fields");return false;} else {return true;}}</script> This way you can have a standard submit type input and whichever way the user submits the form validation will be performed. Link to comment Share on other sites More sharing options...
Sigal Posted November 14, 2007 Author Share Posted November 14, 2007 Thank you all, you've been a great help Link to comment Share on other sites More sharing options...
Sigal Posted November 14, 2007 Author Share Posted November 14, 2007 Uh... the validation needs to be in a function<script type="text/javascript">function formValidate() {if(document.getElementById("phone").value.length == 0 && document.getElementById("fax").value.length == 0 && document.getElementById("email").value.length == 0) {alert("You must fill in at least one of the fields");} else {document.getElementById("form").submit();}}</script> However, it is better to use the form's onsubmit event handler because if done Ingolme's way if the user presses enter on one of the fields the form will be submitted without validation. The input code remains the same, for the form tag have <form action="something.php" method="post" id="form" onsubmit="return formValidate()" /> And in the document head <script type="text/javascript">function formValidate() {if(document.getElementById("phone").value.length == 0 && document.getElementById("fax").value.length == 0 && document.getElementById("email").value.length == 0) {alert("You must fill in at least one of the fields");return false;} else {return true;}}</script> This way you can have a standard submit type input and whichever way the user submits the form validation will be performed. Hi Synook,Maybe you can give me some more help here.What I did before I posted my question is using the toturial so I wrote this code:function validate_required(field,alerttxt){with (field){if (value==null||value=="") {alert(alerttxt);return false}else {return true}}}function validate_form(thisform){with (thisform){if (validate_required(orderedBy,"Oredered By must be filled out!")==false) {orderedBy.focus();return false} if (validate_required(shippingAddr,"Shipping Address must be filled out!")==false) {shippingAddr.focus();return false} if (validate_required(city,"City must be filled out!")==false) {city.focus();return false} if (validate_required(state,"State or Country must be filled out!")==false) {state.focus();return false}if (validate_required(zipCode,"Zip Code must be filled out!")==false) {zipCode.focus();return false} if (validate_required(wedYear,"Year must be filled out!")==false) {wedYear.focus();return false} }}<form name="nonPerson" action="submitpage.htm" method="post" onsubmit="return validate_form(this)">Now I added the function you gave me:if(thisform.getElementById("phone").value.length == 0 && thisform.getElementById("fax").value.length == 0 && thisform.getElementById("email").value.length == 0) {alert("You must fill in at least one of the fields: Phone, Fax or Email address.");return false;} else {return true;}but when I hit submit without filling these fields it doesn't give me an error.Thanks a lot,Sigal Link to comment Share on other sites More sharing options...
Synook Posted November 14, 2007 Share Posted November 14, 2007 Why not? Use boolean operators <form action="something.php" method="post" id="form" onsubmit="return (formValidate() && validate_form(this))" /> Link to comment Share on other sites More sharing options...
Sigal Posted November 14, 2007 Author Share Posted November 14, 2007 Why not? Use boolean operators<form action="something.php" method="post" id="form" onsubmit="return (formValidate() && validate_form(this))" /> Ok, added the IF statement to my original function and all is working now.Thank you, THANK YOU, thank you Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.