Jump to content

Validate form


Recommended Posts

HelloIs there a way I cancheck multiple functions with a onsubmit Here's about how I did it (it's not the whole code as I am not on my pc but that's about what I did): Javascript:

function validName() {if(document.frm_reg.name.value.length < 1) {//some onkeyup code...return false;} else{return true;}}function validMail() {if(....) {return false;} else{return true;}}function checkForm() {if(validName() && validMail()) {return true;} else {return false;}}

HTML:

<form><input type="text" name="name" id="name" onkeyup="validName();"/>//here are some other input tags...<input type="submit" onsubmit="return checkForm();" value="Register"></form>

Thanks!

Link to post
Share on other sites

If you just want to see how they're working use an alert? If not, how do you want to check them?

Link to post
Share on other sites

I checked them by changing the color of the field using the onkeyup()-function, which works fine (it's just not in the code because I'm not at my pc at the moment). But I also want the form not to be sent if there is an error so when I hit the submit button it should again go through all the functions and determine wheter all fields are filled in correctly.That' why I created the function checkForm() which should check if every function returns true.Only if that is the case the form should be submitted.

Link to post
Share on other sites

The onsubmit is placed in the form element, never the submit button, heres an example to check all text inputs

<form action="#" method="post" id="myform" name="myform" onSubmit="return validate_this_form(this)"> <input id="fname" name="fname" type="text" title="First Name" /><input id="lname" name="lname" type="text" title="Last Name" /><input id="email" name="email" type="text" title="Email Address" /><input id="address" name="address" type="text" title="Address" /> <input type="submit" value="Submit Button"  /></form>

function validate_this_form(form_elem){var parent_form=document.getElementById(form_elem.id);valid=true; var errormessage=""; for (i=0;i<parent_form.elements.length;i++)	{    if(parent_form.elements[i].type=="text")		{		if(parent_form.elements[i].value==parent_form.elements[i].defaultValue || parent_form.elements[i].value=="")			{			valid=false;			errormessage+= "Please supply "+parent_form.elements[i].title+"\n";			}		}	}if(!valid)	{	alert(errormessage)	}return valid; }

Edited by dsonesuk
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...