Jump to content

general form validation


smartalco
 Share

Recommended Posts

Well, you could check each one as its being filled in (with the "onchange" event), so that the user will be alerted on the first sign of error. The event itself will be triggered by a certain field, and yout validator will initiate just once, checking only the field that was changed.You are going to have to put the event handler on all fields though. If this is what you mean to avoid, use the "onchange" object in JS' DOM while looping on all fields, rather than the "onchange" attribute directly on form fields.

Link to comment
Share on other sites

Maybe you can just loop through all the <input> and <textarea> elements:<form onsubmit="valid()" ... ...

function valid() {var err = false;var errmessage = "The following fields have not been filled in:\n";var fields = document.getElementsByTagName("input");var areas = document.getElementsByTagName("textarea");for(x=0;x<fields.length;x++) {   if(fields[x].value == "") {	  err = true;	  errmessage += fields[x].name + "\n"   }}for(y=0;y<areas.length;y++) {   if(areas[y].value == "") {	  err = true;	  errmessage += areas[y].name + "\n"   }}if(err) alert(errmessage);return false;}

Link to comment
Share on other sites

that still didn't work :/heres my code

<script type="text/javascript" language="javascript">function valid() {var err = false;var errmessage = "All fields must be filled in.\n";var fields = document.getElementsByTagName("input");var areas = document.getElementsByTagName("textarea");for(x=0;x<fields.length;x++) {   if(fields[x].value == "") {	  err = true;   }}for(y=0;y<areas.length;y++) {   if(areas[y].value == "") {	  err = true;   }}if(err){  alert(errmessage);  return false;}else  return true;</script></head><body><div><center><h2>CERTIFICATED PERSONNEL APPLICATION</h2>(The application MUST be filled out in its entirety for full consideration)</center><form onSubmit="return valid()" name="application" action="" method="post" enctype="multipart/form-data">

PS: sorry for the long time between posts, been busy

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...