473,326 Members | 2,136 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,326 software developers and data experts.

Please Help with form validation

Hi I have this code and it works well, but how do I add another check for valid.Surname ???

Please Help


Expand|Select|Wrap|Line Numbers
  1. // Validator Object
  2.     var valid = new Object();
  3.  
  4.     // REGEX Elements
  5.  
  6.         // matches zip codes
  7.         valid.PostCode = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$/;
  8.  
  9.         //matches email
  10.         valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
  11.  
  12.         // matches phone ###-###-####
  13.         //valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
  14.         valid.ContactNo = /^0\d{2,4}[ -]{1}[\d]{3}[\d -]{1}[\d -]{1}[\d]{1,4}$/;
  15.  
  16.  
  17.     function validateForm(theForm) {
  18.  
  19.         var elArr = theForm.elements; 
  20.  
  21.         for(var i = 0; i < elArr.length; i++) {
  22.  
  23.            with(elArr[i]) { 
  24.  
  25.               var v = elArr[i].validator; 
  26.  
  27.               if(!v) continue; 
  28.  
  29.               var thePat = valid[v]; 
  30.  
  31.               var gotIt = thePat.exec(value); 
  32.  
  33.               if(! gotIt){
  34.                  alert(name + ": Please insert a valid " + v + " to " + value);                  
  35.                  elArr[i].select();
  36.                  elArr[i].focus(); 
  37.                  return false;
  38.               }
  39.            }
  40.         }
  41.  
  42.         return true;
  43.  
  44.     }
May 9 '07 #1
2 1074
acoder
16,027 Expert Mod 8TB
Welcome to TSDN.

Why not just add another regex for surname?
May 9 '07 #2
pbmods
5,821 Expert 4TB
Hi I have this code and it works well, but how do I add another check for valid.Surname ???
Are you trying to apply multiple regexes to one field?

Expand|Select|Wrap|Line Numbers
  1. valid.surname = [
  2.     /regex1/,
  3.     /regex2/
  4. ];
  5.  
  6. .
  7. .
  8. .
  9.  
  10. // AFAIK, this is the most reliable way to check type cross-browser.  Does it work in IE?
  11. if(valid[v].constructor == Array) {
  12.     gotIt = true;
  13.     for(var i = 0; (i < valid[v].length) && gotIt; i++)
  14.         gotIt = valid[v][i].exec(value);
  15. } else
  16.     gotit = valid[v].exec(value);
  17.  
P.S., thanks for using code tags!
May 10 '07 #3

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

Similar topics

4
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when...
72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked to...
6
by: Brynn | last post by:
Looking to get some frustration out ... LOL. I am creating a list of bad words ... I have a pretty good list started ... but want more. Please send some messages to me including bad words in...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
6
by: Ann Duguay | last post by:
Hi there! Newbie here... :-) I'm trying to validate a form but can't seem to get it to work... Here's a copy of the code: The user has to select a color for every select box. How is this...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
2
by: Paul | last post by:
Hi, I hope somebody can help me with the following. (I have tried searching for a solution on the newsgroups but nothing is quite what I need. Feel free to point me in the direction of anything...
2
by: Tim Frawley | last post by:
Source code attached indicates my problem with validation and a button bar save button. Fill the Textbox with some text then tab off the control. The message box will display the text in the...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
2
by: Henry Stock | last post by:
I can understand what these error messages are telling me, but I guess I am not sure how to address them all. The bold tag could be handled in a class attribute, but I need a <br/tag inside the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.