Connecting Tech Pros Worldwide Forums | Help | Site Map

Script to validate all form fields for null in one quick call

Newbie
 
Join Date: Dec 2006
Posts: 6
#1   Jun 22 '07
Hello all,

I am very new to coding and even newer to Javascript. My brain works by thinking in general/abstract all the time and I am lazy, so I hate doing anything remotely repetitious. In that vein, I wrote this script to chuck through a form, any form and check for nulls where there shouldn't be. I used alerts to notify, but you could just as easily add a red error tag and focus() back. All you have to do is add informative and readable titles to fields you want null-validated. And add this to your onsubmit="return validation();" line. All hidden or button or submit, etc. inputs are ignored if you don't add a title to them.

Expand|Select|Wrap|Line Numbers
  1. function nullValidation() {
  2.     var x = document.getElementById("myForm");
  3.  
  4.     for (var i = 0; i < x.length; i++) {
  5.         if(x.elements[i].value == "" && x.elements[i].title) {
  6.             alert("Please enter or select " + x.elements[i].title + ".");
  7.             x.elements[i].focus();
  8.             return false;
  9.         }
  10.     }
  11.  
  12.     return true;
  13. }
  14.  
again, i am not a coder, but this simple script has saved me much, much time on new site i am building with about 20 different forms on it for getting/updating data to mysql.

regards,
oh4real

Last edited by gits; Oct 5 '07 at 08:49 AM. Reason: code formatting for better readability



acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2   Jun 22 '07

re: Script to validate all form fields for null in one quick call


Thanks for sharing!

I've moved this to the Articles section, since it's not a problem, but a solution.

I've added code tags for you (makes it much easier to read).
letmeknow's Avatar
Newbie
 
Join Date: Sep 2007
Posts: 6
#3   Oct 3 '07

re: Script to validate all form fields for null in one quick call


thanks it really saves a lot of time in formvalidation.
Thank you
FullyH3ktik's Avatar
Member
 
Join Date: Sep 2007
Location: NSW, Australia
Posts: 52
#4   Oct 5 '07

re: Script to validate all form fields for null in one quick call


Yes, thanks for sharing, it does save a lot of time compared to what I had
Newbie
 
Join Date: Dec 2006
Posts: 6
#5   Oct 5 '07

re: Script to validate all form fields for null in one quick call


Glad to help! I am King of generic.

Here was another time saver for PHP/MySQL:

If you ever get form submission which needs to update multiple fields in multiple tables like in a user profile, use this script that finds any fields that have changed, and then rips through to MySQL and UPDATES only those fields. I wrote this to go one field at a time, no compound UPDATE statements, since the hassle of gathering all fields from one table would cut down on SQL queries, but got complex on PHP side.

I use this PHP as an object method (one in account.class.php and one in user.class.php), so admins, users, FBI, NSA, anyone who wants to can use it.

http://www.thescripts.com/forum/thread666660.html

oh4real
Reply