SunQuest
 
           Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old March 23rd, 2006, 06:01 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
Click here for more information.
 
Join Date: Sep 2004
Location: Israel
Posts: 26,608 Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 6th Grade (Above 100000 Reputation Level)  Folding Points: 325325 Folding Title: Super Ultimate Folder - Level 1Folding Points: 325325 Folding Title: Super Ultimate Folder - Level 1Folding Points: 325325 Folding Title: Super Ultimate Folder - Level 1Folding Points: 325325 Folding Title: Super Ultimate Folder - Level 1Folding Points: 325325 Folding Title: Super Ultimate Folder - Level 1Folding Points: 325325 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 4 Days 12 h 53 m 47 sec
Reputation Power: 1400
Javascript - Generic form validation

The attached code will validate any form.
all you need to do is include the ShadowValidate.js file in
your page, and have form like in the below example.

comments are below in the ShadowValidate.js code, feel free
to add your comments or questions here as well.

note: this code will not work on older versions of IE and
was tested only on IE6 and Firefox.
also, the required html is not valid xhtml strict.

--ShadowValidate.js
javascript Code:
Original - javascript Code
  1. /*
  2. ShadowValidation
  3. ----------------
  4. Generic Form Validation.
  5. To activate this validation, add attributes to your form elements.
  6. Attribute "validate" defines what type of validation you want.
  7. Possible values for "validate" attribute:
  8.     "not_empty" - element can't be empty.
  9.     "integer" - element must have integer value.
  10.     "number" - element must have numeric value.
  11.     "email" - element value must be valid email address.
  12.     "alphabet" - element value must consist only of given alphabet list in allow_only attribute.
  13.             you can give ranges using ".." in the allow_only attribute.
  14.     [function name] - vaidate using your own function. see below for more details.
  15. You can join more than one validation type by using the "|" character.
  16. For example:
  17.     <input type="text" name="myinput" validate="not_empty|number" />
  18.     will force not empty and numeric value.
  19. Example for alphabet validation:
  20.     <input type="text" name="myinput" validate="not_empty|alphabet" allow_only="a..zA..Z0..9" />
  21.     will force not empty value that contains only english letters or digits.
  22. Attribute "msg" defines the message to display when validation fails.
  23. For example:
  24.     <input type="text" name="myinput" validate="not_empty|number" msg="please enter value|invalid value" />
  25.     will show "please enter value" if value is empty and "invalid value" if not numeric.
  26. By default, the message will appear next to the form element.
  27. You can have the message appear as alert dialog by adding show_alert attribute to the form.
  28. For example:
  29.     <form show_alert="1" onsubmit="return Validate(this);">
  30.     will show the messages as alerts.
  31. Attribute "error_container" defines where the message will appear in case of invalid input.
  32. You must have corresponding <span> or <div> tag with that exact ID in the page. If this
  33. attribute is missing, the message will appear after the input element.
  34. For example:
  35.     <input type="radio" name="gender" value="M" validate="not_empty" msg="please select gender" error_container="GenderErrorContainer" />
  36.     and then have such code after the radio buttons:
  37.     <span id="GenderErrorContainer"></span>
  38. Advanced:
  39.     You can use your own function to validate the data.
  40.     For this, write the function name as the value of "validate" attribute.
  41.     For example:
  42.         <input type="text" name="myinput" validate="MyValidate" />
  43.     The function must get the form element as its parameter and return true if valid or false otherwise.
  44. Written by:
  45.     © Shadow Wizard, 2006
  46. */
  47.  
  48. function Validate(objForm) {
  49.     var arrValidated=new Array();
  50.    
  51.     for (var i=0; i<objForm.elements.length; i++) {
  52.         var element=objForm.elements[i];
  53.         var elName=element.name;
  54.         if ((!elName)||(elName.length == 0)||(arrValidated[elName]))
  55.             continue;
  56.         arrValidated[elName] = true;
  57.         var validationType = element.getAttribute("validate");
  58.         if ((!validationType)||(validationType.length == 0))
  59.             continue;
  60.         var strMessages=element.getAttribute("msg");
  61.         if (!strMessages)
  62.             strMessages = "";
  63.         var arrMessages = strMessages.split("|");
  64.         var arrValidationTypes = validationType.split("|");  
  65.         for (var j=0; j<arrValidationTypes.length; j++) {
  66.             var curValidationType = arrValidationTypes[j];
  67.             var blnValid=true;
  68.             switch (curValidationType) {
  69.                 case "not_empty":
  70.                     blnValid = ValidateNotEmpty(element);
  71.                     break;
  72.                 case "integer":
  73.                     blnValid = ValidateInteger(element);
  74.                     break;
  75.                 case "number":
  76.                     blnValid = ValidateNumber(element);
  77.                     break;
  78.                 case "email":
  79.                     blnValid = ValidateEmail(element);
  80.                     break;
  81.                 case "alphabet":
  82.                     blnValid = ValidateAlphaBet(element);
  83.                     break;
  84.                 default:
  85.                     try {
  86.                         blnValid = eval(curValidationType+"(element)");
  87.                     }
  88.                     catch (ex) {
  89.                         blnValid = true;
  90.                     }
  91.             }
  92.             if (blnValid == false) {
  93.                 var message="invalid value for "+element.name;
  94.                 if ((j < arrMessages.length)&&(arrMessages[j].length > 0))
  95.                     message = arrMessages[j];
  96.                 InsertError(element, message);
  97.                 if ((typeof element.focus == "function")||(element.focus)) {
  98.                     element.focus();
  99.                 }
  100.                 return false;
  101.             }
  102.             else
  103.                 ClearError(element);
  104.         }
  105.        
  106.     }
  107.    
  108.     return true;
  109. }
  110.  
  111. function ValidateNotEmpty(objElement) {
  112.     var strValue = GetElementValue(objElement);
  113.     return (strValue.length > 0);
  114. }
  115.  
  116. function ValidateInteger(objElement) {
  117.     var strValue = GetElementValue(objElement);
  118.     return (!isNaN(parseInt(strValue)));
  119. }
  120.  
  121. function ValidateNumber(objElement) {
  122.     var strValue = GetElementValue(objElement);
  123.     return (!isNaN(parseFloat(strValue)));
  124. }
  125.  
  126. function ValidateEmail(objElement) {
  127.     var strValue = GetElementValue(objElement);
  128.     if (strValue.length < 5)
  129.         return false;
  130.     var arrTemp=strValue.split("@");
  131.     if (arrTemp.length != 2)
  132.         return false;
  133.     var strLeftPart = arrTemp[0];
  134.     var strRightPart = arrTemp[1];
  135.     if ((strLeftPart.length == 0)||(strRightPart.length == 0))
  136.         return false;
  137.     arrTemp = strRightPart.split(".");
  138.     if (arrTemp.length < 2)
  139.         return false;
  140.     for (var i=0; i<arrTemp.length; i++) {
  141.         if (arrTemp[i].length == 0)
  142.             return false;
  143.     }
  144.     return true;
  145. }
  146.  
  147. function ValidateAlphaBet(objElement) {
  148.     var strValue = GetElementValue(objElement);
  149.    
  150.     if (strValue.length == 0)
  151.         return true;
  152.    
  153.     var strAllowOnly = objElement.getAttribute("allow_only");
  154.     if (!strAllowOnly)
  155.         strAllowOnly = "";
  156.    
  157.     if (strAllowOnly.length == 0)
  158.         return true;
  159.    
  160.     var i=0;
  161.     var arrAllowedChars=new Array();
  162.     while (i < strAllowOnly.length) {
  163.         if ((i < (strAllowOnly.length-2)) && (strAllowOnly.substr(i+1, 2) == "..")) {
  164.             for (var j=strAllowOnly.charCodeAt(i); j<=strAllowOnly.charCodeAt(i+3); j++) {
  165.                 arrAllowedChars[arrAllowedChars.length] = String.fromCharCode(j);
  166.             }
  167.             i += (2*2);
  168.             continue;
  169.         }
  170.         arrAllowedChars[arrAllowedChars.length] = strAllowOnly.charAt(i)+"";
  171.         i++;
  172.     }
  173.    
  174.     for (var i=0; i<strValue.length; i++)
  175.         if (InArray(arrAllowedChars, strValue.substr(i, 1)) < 0)
  176.             return false;
  177.    
  178.     return true;
  179. }
  180.  
  181. function GetElementValue(objElement) {
  182.     var result="";
  183.     switch (objElement.type) {
  184.         case "text":
  185.         case "hidden":
  186.         case "textarea":
  187.         case "password":
  188.             result = objElement.value;
  189.             break;
  190.         case "select-one":
  191.         case "select":
  192.             if (objElement.selectedIndex >= 0)
  193.                 result = objElement.options[objElement.selectedIndex].value;
  194.             break;
  195.         case "radio":
  196.         case "checkbox":
  197.             for (var i=0; i<objElement.form.elements.length; i++) {
  198.                 if (objElement.form.elements[i].name == objElement.name) {
  199.                     if (objElement.form.elements[i].checked)
  200.                         result += objElement.form.elements[i].value+",";
  201.                 }
  202.             }
  203.             break;
  204.     }
  205.     return result;
  206. }
  207.  
  208. function InsertError(element, strMessage) {
  209.     if ((element.form.getAttribute("show_alert")) && (element.form.getAttribute("show_alert") != "0")) {