smartalco Posted December 7, 2007 Share Posted December 7, 2007 Is there a way to use javascript to check if all values on a form are filled out without checking each one? (the form I'm working on has over 50 fields, checking that many gets ridiculous) Link to comment Share on other sites More sharing options...
boen_robot Posted December 7, 2007 Share Posted December 7, 2007 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 More sharing options...
Ingolme Posted December 8, 2007 Share Posted December 8, 2007 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 More sharing options...
smartalco Posted December 13, 2007 Author Share Posted December 13, 2007 ^that unfortunately didn't work, i know it calls the function, the function itself isn't doing anything though :/anyone else have any ideas? Link to comment Share on other sites More sharing options...
aspnetguy Posted December 13, 2007 Share Posted December 13, 2007 you should have the following if you want it to stop the submit if things are not valid <form onsubmit="return valid()" Link to comment Share on other sites More sharing options...
justsomeguy Posted December 13, 2007 Share Posted December 13, 2007 That function always returns false, it will never submit. Change the last lines from this:if(err) alert(errmessage);return false;To this: if(err) { alert(errmessage); return false;}else return true; Link to comment Share on other sites More sharing options...
smartalco Posted December 17, 2007 Author Share Posted December 17, 2007 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 More sharing options...
aalbetski Posted December 17, 2007 Share Posted December 17, 2007 your function is missing its closing brace Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.