473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to validate different HTML forms using the same script.

250 100+
I want to validate my php site. I created javascript file for the validate functions. Eg:

Expand|Select|Wrap|Line Numbers
  1. function validateFormOnSubmit(theForm) {
  2.  
  3. var reason = "";
  4.  
  5.  
  6.  
  7.   reason += validateEmpty(theForm.from);
  8.  
  9.   reason += validateUsername(theForm.username);
  10.  
  11.   reason += validatePassword(theForm.pwd);
  12.  
  13.   reason += validateEmail(theForm.email);
  14.  
  15.   reason += validatePhone(theForm.phone);
  16.  
  17.  
  18.  
  19.  
  20.  
  21.   if (reason != "") {
  22.  
  23.     alert("Some fields need correction:\n" + reason);
  24.  
  25.     return false;
  26.  
  27.   }
  28.  
  29.  
  30.  
  31.   return true;
  32.  
  33. }
  34.  
  35. // validate empty fields
  36.  
  37. function validateEmpty(fld) {
  38.  
  39.     var error = "";
  40.  
  41.  
  42.  
  43.     if (fld.value.length == 0) {
  44.  
  45.         fld.style.background = 'Yellow'; 
  46.  
  47.         error = "The required field has not been filled in.\n"
  48.  
  49.     } else {
  50.  
  51.         fld.style.background = 'White';
  52.  
  53.     }
  54.  
  55.     return error;  
  56.  
  57. }

But the thing is different pages have different number of fields.(page 1 have 5 text boxes, page 2 have 10 text boxes etc.). So could you please tell me how can I use the same validate file to every page?
Jun 10 '09 #1
4 2317
code green
1,726 Expert 1GB
You could collect the fields into an array and loop through them
Maybe have a hidden HTML variable on each form specifying how many fields there are.

This means naming the form elements elem1 elem2 elem3 so the function can handle them dynamically.
ie psuedocode
Expand|Select|Wrap|Line Numbers
  1. size = sizeof(theForm.elements[]);
  2. for(c=1;c<size;c++)
  3. {
  4.     //pass to function
  5.    function(theForm.elements['elem'.c];
  6.    function(theForm.elements['elem'.c];
  7. }
Sorry can't quite remember how HTML arrays work and my javascript is shaky
Jun 10 '09 #2
Dormilich
8,658 Expert Mod 8TB
@code green
not at all, there are none ;)

back to the problem. It depends on which form elements need checking.

easiest case: all form elements need checking, then loop through theForm.elements[]

if all but one or two, you can exclude them inside the loop (if you know which ones)

you could also group all elements to be checked either with a common class name or a common parent element (e.g. <fieldset>) and use that to select those.
Jun 10 '09 #3
code green
1,726 Expert 1GB
not at all, there are none
Yup,I am thinking of URL variable arrays.
But the genaral idea is to name the form elements in such a way that they can be reffered to dynamically
Expand|Select|Wrap|Line Numbers
  1. size = theForm.noOfElements; //Hidden variable
  2. for(c=1;c<size;c++) 
  3.     //pass to function 
  4.    function(theForm.'elem'.c); 
  5.    function(theForm.'elem'.c); 
Jun 10 '09 #4
Atli
5,058 Expert 4TB
Moved to the Javascript forum.
Jun 11 '09 #5

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

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting 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...
2
by: JR | last post by:
Hi. I have a CGI script that will need to call itself an unknown number of times, to add rows, run queries, etc. At the bottom of the output that is produced by the script, there are four...
2
by: Dave | last post by:
I have a page which uses JavaScript to create form elements using document.createElement('input'), etc.. Both Firefox and IE have no problem accomplishing this and when the form is submitted all...
2
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
4
by: Sathyaish | last post by:
I am no JavaScript progammer, and unfortunately am having to babysit an old code base that has a lot of JavaScript in it. I have two questions: (1) Can two HTML controls have the same name? It...
6
by: Jason | last post by:
I have a sticky problem relating to my 'join' registration form inside our authenticatin system... We have just signed up for salesforce.com and I need to somehow integrate the canned...
26
by: webrod | last post by:
Hi, I have some php pages with a lot of HTML code. I am looking for a HTML validator tool (like TIDY). TIDY is not good enough with PHP tags (it removes a lot of php code). Do you have any...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...

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.