Current location: Hot Scripts Forums » General Web Coding » JavaScript » [SOLVED] Newbie struggling with form validation


[SOLVED] Newbie struggling with form validation

Reply
  #1 (permalink)  
Old 10-18-08, 01:56 AM
Meginoz Meginoz is offline
New Member
 
Join Date: Oct 2008
Location: Tasmania, Australia
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Newbie struggling with form validation

Hi folks,

Background: I'm studying web design, but have no programming experience and have been battling with javascript for months. I've been trying to learn basic form validation and running myself around in circles with various tutorials that all seem to do it differently.

Today I've been working right through a script from the Visual Quickstart Guide to Javascript & Ajax, commenting it as I go to help me understand it. I got it working for checking if required fields are filled in, but I cannot get the email validation part to work. I have copied the code from the book's website (http://www.javascriptworld.com/chapter7.html), but as the book gradually adds things to the code, some of which I don't need, I think I might have removed or left off something that is required (though I've checked it dozens of times).

I ran it through the JSLint validator (don't know if that is a good tool or not), and fixed a few missing semicolons, etc, and there were a bunch of things I didn't really understand, but don't think they're the problem as they all exist in the working code. If someone can have a look and tell me if I'm missing something, I'd be really grateful.

Here is my code:

javascript Code:
  1. //Script courtesy of [url]http://javasacriptworld.com[/url]
  2. window.onload = initForms;
  3.  
  4. //Loops through every form on the page and adds an event handler  to the forms onsubmit - call to function return validForm
  5. function initForms() {
  6.     for (var i=0; i< document.forms.length; i++) {
  7.         document.forms[i].onsubmit = function() {return validForm();};
  8.     }
  9. }
  10.  
  11. function validForm() {
  12.     var allGood = true;
  13.     //returns an array containing all tags on the page
  14.     var allTags = document.getElementsByTagName("*");
  15.    
  16.     //searches through all tags and calls validTag function, which checks to see if there's anything wrong
  17.     //if anything is wrong with a tag, it sets allGood to false, but still keeps looking at all the tags
  18.     for (var i=0; i<allTags.length; i++) {
  19.         if (!validTag(allTags[i])) {
  20.             allGood = false;
  21.         }
  22.     }
  23.     return allGood;
  24.    
  25.     function validTag(thisTag) {
  26.         var outClass = "";
  27.         //creates an array of all the class names for the tag, by splitting the class attribute at each space
  28.         var allClasses = thisTag.className.split(" ");
  29.         //checks all the class attributes and calls the validBasedOnClass function (described below).  Adds whatever that function returns, plus a space to the outClass variable
  30.         for (var j=0; j<allClasses.length; j++) {
  31.             outClass += validBasedOnClass(allClasses[j]) + " ";
  32.         }
  33.         //puts the contents of outClass and puts it into the class for this tag
  34.         thisTag.className = outClass;
  35.    
  36.     //If the word "invalid" is returned in the new class attribute, put focus on that field
  37.     if (outClass.indexOf("invalid") > -1) {
  38.         invalidLabel(thisTag.parentNode);
  39.             thisTag.focus();
  40.             //if this is an input tag, select it
  41.             if (thisTag.nodeName == "INPUT") {
  42.                 thisTag.select();
  43.             }
  44.             return false;
  45.         }
  46.         return true;
  47.        
  48.         function validBasedOnClass(thisClass) {
  49.             var classBack = "";
  50.             //look at the class attribute that was passed, and if it was "invalid"  or empty, break out of the conditional.  Otherwise keep going.
  51.             switch(thisClass) {
  52.                 case "":
  53.                 case "invalid":
  54.                     break;
  55.                 //if the attribute is "reqd", and allGood is true, and the tag is empty, set classBack to invalid (which puts the "invalid" class on the tag
  56.                 case "reqd":
  57.                     if (allGood && thisTag.value == "") classBack = "invalid ";
  58.                     classBack += thisClass;
  59.                     break;
  60.                 case "email":
  61.                     if (allGood && !validEmail(thisTag.value)) classBack = "invalid ";
  62.                     classBack += thisClass;
  63.                     break;
  64.                                        
  65.                 default:
  66.                     classBack += thisClass;
  67.             }
  68.             return classBack;
  69.         }
  70.    
  71.     function validEmail(email) {
  72.             var invalidChars = " /:,;";
  73.        
  74.             if (email == "") {
  75.                 return false;
  76.             }
  77.             for (var k=0; k<invalidChars.length; k++) {
  78.                 var badChar = invalidChars.charAt(k);
  79.                 if (email.indexOf(badChar) > -1) {
  80.                     return false;
  81.                 }
  82.             }
  83.             var atPos = email.indexOf("@",1);
  84.             if (atPos == -1) {
  85.                 return false;
  86.             }
  87.             if (email.indexOf("@",atPos+1) != -1) {
  88.                 return false;
  89.             }
  90.             var periodPos = email.indexOf(".",atPos);
  91.             if (periodPos == -1) { 
  92.                 return false;
  93.             }
  94.             if (periodPos+3 > email.length) {
  95.                 return false;
  96.             }
  97.             return true;
  98.         }
  99.         function invalidLabel(parentTag) {
  100.             if (parentTag.nodeName == "LABEL") {
  101.                 parentTag.className += " invalid";
  102.             }
  103.         }
  104.     }
  105. }

And the HTML:

html4strict Code:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  4.     <title>Contact Bazaar</title>
  5.     <script type="text/javascript" src="reqfields.js">
  6.     </script>
  7.     <link rel="stylesheet" href="reqfields.css" />
  8. </head>
  9.  
  10.  
  11. <form method="post" action="emailform.php">
  12. <legend>Contact Bazaar Ceramics</legend>
  13. <label for="name">Your Name</label><input type="text" name="name" id="name" class="reqd"/><br />
  14. <label for="phone">Phone number</label><input type="text" name="phone" id="phone" /><br />
  15. <label for="email">Email address</label><input type="text" name="email" id="email" class="reqd"/><br />
  16. <label for="comments">Comments</label><textarea cols="20" name="comments" id="comments" rows="5" class="reqd"></textarea><br />
  17. </fieldset>
  18. <fieldset class="submit">
  19. <input type="submit" value="Send" class="buttons" />
  20. </fieldset>
  21. </form>
  22.  
  23.  
  24. </body>
  25. </html>
Reply With Quote
  #2 (permalink)  
Old 10-18-08, 08:19 PM
Meginoz Meginoz is offline
New Member
 
Join Date: Oct 2008
Location: Tasmania, Australia
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Newbie struggling with form validation

I finally realised that the email field in the HTML needed to have a class of "email" attached to it. I hadn't worked out that it was using the class, not the id, to determine which field to apply the validation to.

I'm sure I'll be back very soon as I work through the next bits.

Edit: Sorry, I can't figure out how to get SOLVED into the thread title that shows up on the board.

Last edited by Meginoz; 10-18-08 at 08:27 PM.
Reply With Quote
  #3 (permalink)  
Old 10-19-08, 03:14 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
Originally Posted by Meginoz View Post
Edit: Sorry, I can't figure out how to get SOLVED into the thread title that shows up on the board.
Take a look at this announcement: http://www.programmingtalk.com/announcement.php?a=16
Reply With Quote
  #4 (permalink)  
Old 10-19-08, 03:29 AM
Meginoz Meginoz is offline
New Member
 
Join Date: Oct 2008
Location: Tasmania, Australia
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Ah, thanks! I did hunt around, but didn't find that. Now I know.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ajax checking and onsubmit issue follower JavaScript 4 10-12-08 03:45 PM
Form Validation slimey PHP 10 03-20-06 06:23 PM
Newbie needs help using form mailer script Charmaine99 PHP 3 03-11-06 07:35 PM
Need help with form validation script dathandawg JavaScript 1 12-29-05 12:17 AM
Flexible form validation question epetoke JavaScript 6 09-12-04 04:19 PM


All times are GMT -5. The time now is 11:44 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.