Jump to content

Validate Form


Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites
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 post
Share on other sites

Why not? Use boolean operators

<form action="something.php" method="post" id="form" onsubmit="return (formValidate() && validate_form(this))" />

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...