Jump to content

JavaScript form help


Natechs

Recommended Posts

Hey.On my Services page, I have a JavaScript script that checks that of the form elements are filled in. But when the user doesn't fill in a form element the form still processes. Is there a way so that if a user hasn't filled in a field, the form won't process.java script:

function checkform() {  var f = document.requests;    for(var i = 0; i < f.elements.length; i++) {      if (f.elements[0].type != "text") continue;        var text = f.elements[i].value;         if (text == null || text.length == 0) {           alert("Please fill in all of the fields");             return true;    }  }}

Form code:

<form action="form.php" method="post" id="requests" name="requests" onsubmit="checkform()" onreset="confirm('Do you really want to reset the form?')"><div><p>Name:</p><input type="text" name="name" value="" class="text" /><p>Email address:</p><input type="text" name="email" value="" class="text" /><p>What do you want the background-color to be?</p><input type="text" name="bgcolor" value="" class="text" /><p>Font color?</p><input type="text" name="fcolor" value="" class="text" /><p>Fonts?</p><input type="text" name="fonts" value="" class="text" /><p>What is your page title?</p><input type="text" name="title" value="" class="text" /><p>Images? (provide the URLs)</p><input type="text" name="images" value="" class="text" /><p>Provide any other info:</p><textarea cols="50" rows="15" name="otherinfo" class="text">Put the following in this field: Layout: One column, two column, or three column.Menu: Vertical or horizontal.Links: What links you want in the menu.Any other things you want me to know about your request.</textarea><br /><input type="submit" value="Submit my Request" class="text" /><input type= "reset" value="Reset" class="text" /></div></form>

Link to comment
Share on other sites

Try it this way:

function checkform() {   var f = document.getElementById("requests");   for(var i = 0; i < f.elements.length; i++) {	  if (f.elements[0].type != "text") continue; // SHOULD THAT "0" be "i" ???	  var text = f.elements[i].value;	  if (text == null || text.length == 0) {		 alert("Please fill in all of the fields");		 return false;	  }   }  return true;}

AND

<form action="form.php" method="post" id="requests" name="requests" onsubmit="return checkform()" onreset="confirm('Do you really want to reset the form?')">

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...