473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,394 developers and data experts.

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

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
Jun 22 '07 #1
4 11761
acoder
16,027 Expert Mod 8TB
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).
Jun 22 '07 #2
thanks it really saves a lot of time in formvalidation.
Thank you
Oct 3 '07 #3
Yes, thanks for sharing, it does save a lot of time compared to what I had
Oct 5 '07 #4
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
Oct 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Don | last post by:
I want the server-side php script to return a browser page that is essentially a copy of the original client page that contained the <form> which referenced the php script in the first place....
6
by: Andrew V. Romero | last post by:
First off, I am mostly familier with PHP but am trying to make a multi-step javascript program. For an example, I would like to run different sections of the script depending on how many times the...
5
by: Mike | last post by:
I'm using a script provided by e-mailanywhere, it's a little too big for me. There's 1 text field and 1 password field in a form. OnSubmit, I would like both fields to be validated to look for...
7
by: Dr. Know | last post by:
I am working on an ASP page that writes to several databases, ranging from MDBs to x-base. One of the tasks involves using an existing highest value from the DB and incrementing it before...
11
by: Jozef | last post by:
I have some old code that I use from the Access 95 Developers handbook. The code works very well, with the exception that it doesn't seem to recognize wide screens, and sizes tab controls so that...
9
by: B-Dog | last post by:
I have a form that has about 10 text boxes on it, they all have to be filled out before submitting is there a quick way to make sure that none are null or do I have to call out each textbox? Say...
1
by: Rollie72 | last post by:
Hello, This is a first time post for me. Have this HTML form being validated by a javascript. I am having a problem with the line "document.ACLinfo.BUmanager.value=manager;" . When in Firefox...
5
nathj
by: nathj | last post by:
Hi, I have been looking around the forum and the web for a way to achieve this and so far I have drawn a blank. So I head to the forum as I'm sure someone knows how to do this. I have a form...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.