Subdomain Posts
None | 18 hours ago
None | 18 hours ago
None | 761 days ago
None | 761 days ago
None | 787 days ago
Recent Posts
None | 17 sec ago
None | 19 sec ago
None | 26 sec ago
None | 28 sec ago
None | 31 sec ago
None | 33 sec ago
C++ | 41 sec ago
None | 1 min ago
C | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By matt 2 on the 18th of Feb 2008 04:30:04 PM Download | Raw | Embed | Report
  1. /*
  2.   -------------------------------------------------------------------------
  3.                             JavaScript Form Validator
  4.                                 Version 2.0.2
  5.         Copyright 2003 JavaScript-coder.com. All rights reserved.
  6.         You use this script in your Web pages, provided these opening credit
  7.     lines are kept intact.
  8.         The Form validation script is distributed free from JavaScript-Coder.com
  9.  
  10.         You may please add a link to JavaScript-Coder.com,
  11.         making it easy for others to find this script.
  12.         Checkout the Give a link and Get a link page:
  13.         http://www.javascript-coder.com/links/how-to-link.php
  14.  
  15.     You may not reprint or redistribute this code without permission from
  16.     JavaScript-Coder.com.
  17.        
  18.         JavaScript Coder
  19.         It precisely codes what you imagine!
  20.         Grab your copy here:
  21.                 http://www.javascript-coder.com/
  22.     -------------------------------------------------------------------------  
  23. */
  24. function Validator(frmname)
  25. {
  26.   this.formobj=document.forms[frmname];
  27.         if(!this.formobj)
  28.         {
  29.           alert("BUG: couldnot get Form object "+frmname);
  30.                 return;
  31.         }
  32.         if(this.formobj.onsubmit)
  33.         {
  34.          this.formobj.old_onsubmit = this.formobj.onsubmit;
  35.          this.formobj.onsubmit=null;
  36.         }
  37.         else
  38.         {
  39.          this.formobj.old_onsubmit = null;
  40.         }
  41.         this.formobj.onsubmit=form_submit_handler;
  42.         this.addValidation = add_validation;
  43.         this.setAddnlValidationFunction=set_addnl_vfunction;
  44.         this.clearAllValidations = clear_all_validations;
  45. }
  46. function set_addnl_vfunction(functionname)
  47. {
  48.   this.formobj.addnlvalidation = functionname;
  49. }
  50. function clear_all_validations()
  51. {
  52.         for(var itr=0;itr < this.formobj.elements.length;itr++)
  53.         {
  54.                 this.formobj.elements[itr].validationset = null;
  55.         }
  56. }
  57. function form_submit_handler()
  58. {
  59.         for(var itr=0;itr < this.elements.length;itr++)
  60.         {
  61.                 if(this.elements[itr].validationset &&
  62.            !this.elements[itr].validationset.validate())
  63.                 {
  64.                   return false;
  65.                 }
  66.         }
  67.         if(this.addnlvalidation)
  68.         {
  69.           str =" var ret = "+this.addnlvalidation+"()";
  70.           eval(str);
  71.     if(!ret) return ret;
  72.         }
  73.  
  74.         return true;
  75. }
  76. function add_validation(itemname,descriptor,errstr)
  77. {
  78.   if(!this.formobj)
  79.         {
  80.           alert("BUG: the form object is not set properly");
  81.                 return;
  82.         }//if
  83.         var itemobj = this.formobj[itemname];
  84.   if(!itemobj)
  85.         {
  86.           alert("BUG: Couldnot get the input object named: "+itemname);
  87.                 return;
  88.         }
  89.         if(!itemobj.validationset)
  90.         {
  91.           itemobj.validationset = new ValidationSet(itemobj);
  92.         }
  93.   itemobj.validationset.add(descriptor,errstr);
  94. }
  95. function ValidationDesc(inputitem,desc,error)
  96. {
  97.   this.desc=desc;
  98.         this.error=error;
  99.         this.itemobj = inputitem;
  100.         this.validate=vdesc_validate;
  101. }
  102. function vdesc_validate()
  103. {
  104.  if(!V2validateData(this.desc,this.itemobj,this.error))
  105.  {
  106.     this.itemobj.focus();
  107.                 return false;
  108.  }
  109.  return true;
  110. }
  111. function ValidationSet(inputitem)
  112. {
  113.     this.vSet=new Array();
  114.         this.add= add_validationdesc;
  115.         this.validate= vset_validate;
  116.         this.itemobj = inputitem;
  117. }
  118. function add_validationdesc(desc,error)
  119. {
  120.   this.vSet[this.vSet.length]=
  121.           new ValidationDesc(this.itemobj,desc,error);
  122. }
  123. function vset_validate()
  124. {
  125.    for(var itr=0;itr<this.vSet.length;itr++)
  126.          {
  127.            if(!this.vSet[itr].validate())
  128.                  {
  129.                    return false;
  130.                  }
  131.          }
  132.          return true;
  133. }
  134. function validateEmailv2(email)
  135. {
  136. // a very simple email validation checking.
  137. // you can add more complex email checking if it helps
  138.     if(email.length <= 0)
  139.         {
  140.           return true;
  141.         }
  142.     var splitted = email.match("^(.+)@(.+)$");
  143.     if(splitted == null) return false;
  144.     if(splitted[1] != null )
  145.     {
  146.       var regexp_user=/^\"?[\w-_\.]*\"?$/;
  147.       if(splitted[1].match(regexp_user) == null) return false;
  148.     }
  149.     if(splitted[2] != null)
  150.     {
  151.       var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
  152.       if(splitted[2].match(regexp_domain) == null)
  153.       {
  154.             var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
  155.             if(splitted[2].match(regexp_ip) == null) return false;
  156.       }// if
  157.       return true;
  158.     }
  159. return false;
  160. }
  161. function V2validateData(strValidateStr,objValue,strError)
  162. {
  163.     var epos = strValidateStr.search("=");
  164.     var  command  = "";
  165.     var  cmdvalue = "";
  166.     if(epos >= 0)
  167.     {
  168.      command  = strValidateStr.substring(0,epos);
  169.      cmdvalue = strValidateStr.substr(epos+1);
  170.     }
  171.     else
  172.     {
  173.      command = strValidateStr;
  174.     }
  175.     switch(command)
  176.     {
  177.         case "req":
  178.         case "required":
  179.          {
  180.            if(eval(objValue.value.length) == 0)
  181.            {
  182.               if(!strError || strError.length ==0)
  183.               {
  184.                 strError = objValue.name + " : Required Field";
  185.               }//if
  186.               alert(strError);
  187.               return false;
  188.            }//if
  189.            break;            
  190.          }//case required
  191.         case "maxlength":
  192.         case "maxlen":
  193.           {
  194.              if(eval(objValue.value.length) >  eval(cmdvalue))
  195.              {
  196.                if(!strError || strError.length ==0)
  197.                {
  198.                  strError = objValue.name + " : "+cmdvalue+" characters maximum ";
  199.                }//if
  200.                alert(strError + "\n[Current length = " + objValue.value.length + " ]");
  201.                return false;
  202.              }//if
  203.              break;
  204.           }//case maxlen
  205.         case "minlength":
  206.         case "minlen":
  207.            {
  208.              if(eval(objValue.value.length) <  eval(cmdvalue))
  209.              {
  210.                if(!strError || strError.length ==0)
  211.                {
  212.                  strError = objValue.name + " : " + cmdvalue + " characters minimum  ";
  213.                }//if              
  214.                alert(strError + "\n[Current length = " + objValue.value.length + " ]");
  215.                return false;                
  216.              }//if
  217.              break;
  218.             }//case minlen
  219.         case "alnum":
  220.         case "alphanumeric":
  221.            {
  222.               var charpos = objValue.value.search("[^A-Za-z0-9]");
  223.               if(objValue.value.length > 0 &&  charpos >= 0)
  224.               {
  225.                if(!strError || strError.length ==0)
  226.                 {
  227.                   strError = objValue.name+": Only alpha-numeric characters allowed ";
  228.                 }//if
  229.                 alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
  230.                 return false;
  231.               }//if
  232.               break;
  233.            }//case alphanumeric
  234.         case "num":
  235.         case "numeric":
  236.            {
  237.               var charpos = objValue.value.search("[^0-9]");
  238.               if(objValue.value.length > 0 &&  charpos >= 0)
  239.               {
  240.                 if(!strError || strError.length ==0)
  241.                 {
  242.                   strError = objValue.name+": Only digits allowed ";
  243.                 }//if              
  244.                 alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
  245.                 return false;
  246.               }//if
  247.               break;              
  248.            }//numeric
  249.         case "alphabetic":
  250.         case "alpha":
  251.            {
  252.               var charpos = objValue.value.search("[^A-Za-z]");
  253.               if(objValue.value.length > 0 &&  charpos >= 0)
  254.               {
  255.                   if(!strError || strError.length ==0)
  256.                 {
  257.                   strError = objValue.name+": Only alphabetic characters allowed ";
  258.                 }//if                            
  259.                 alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
  260.                 return false;
  261.               }//if
  262.               break;
  263.            }//alpha
  264.                 case "alnumhyphen":
  265.                         {
  266.               var charpos = objValue.value.search("[^A-Za-z0-9\-_]");
  267.               if(objValue.value.length > 0 &&  charpos >= 0)
  268.               {
  269.                   if(!strError || strError.length ==0)
  270.                 {
  271.                   strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _";
  272.                 }//if                            
  273.                 alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
  274.                 return false;
  275.               }//if                    
  276.                         break;
  277.                         }
  278.         case "email":
  279.           {
  280.                if(!validateEmailv2(objValue.value))
  281.                {
  282.                  if(!strError || strError.length ==0)
  283.                  {
  284.                     strError = objValue.name+": Enter a valid Email address ";
  285.                  }//if                                              
  286.                  alert(strError);
  287.                  return false;
  288.                }//if
  289.            break;
  290.           }//case email
  291.         case "lt":
  292.         case "lessthan":
  293.          {
  294.             if(isNaN(objValue.value))
  295.             {
  296.               alert(objValue.name+": Should be a number ");
  297.               return false;
  298.             }//if
  299.             if(eval(objValue.value) >=  eval(cmdvalue))
  300.             {
  301.               if(!strError || strError.length ==0)
  302.               {
  303.                 strError = objValue.name + " : value should be less than "+ cmdvalue;
  304.               }//if              
  305.               alert(strError);
  306.               return false;                
  307.              }//if            
  308.             break;
  309.          }//case lessthan
  310.         case "gt":
  311.         case "greaterthan":
  312.          {
  313.             if(isNaN(objValue.value))
  314.             {
  315.               alert(objValue.name+": Should be a number ");
  316.               return false;
  317.             }//if
  318.              if(eval(objValue.value) <=  eval(cmdvalue))
  319.              {
  320.                if(!strError || strError.length ==0)
  321.                {
  322.                  strError = objValue.name + " : value should be greater than "+ cmdvalue;
  323.                }//if              
  324.                alert(strError);
  325.                return false;                
  326.              }//if            
  327.             break;
  328.          }//case greaterthan
  329.         case "regexp":
  330.          {
  331.                         if(objValue.value.length > 0)
  332.                         {
  333.                     if(!objValue.value.match(cmdvalue))
  334.                     {
  335.                       if(!strError || strError.length ==0)
  336.                       {
  337.                         strError = objValue.name+": Invalid characters found ";
  338.                       }//if                                                              
  339.                       alert(strError);
  340.                       return false;                  
  341.                     }//if
  342.                         }
  343.            break;
  344.          }//case regexp
  345.         case "dontselect":
  346.          {
  347.             if(objValue.selectedIndex == null)
  348.             {
  349.               alert("BUG: dontselect command for non-select Item");
  350.               return false;
  351.             }
  352.             if(objValue.selectedIndex == eval(cmdvalue))
  353.             {
  354.              if(!strError || strError.length ==0)
  355.               {
  356.               strError = objValue.name+": Please Select one option ";
  357.               }//if                                                              
  358.               alert(strError);
  359.               return false;                                  
  360.              }
  361.              break;
  362.          }//case dontselect
  363.     }//switch
  364.     return true;
  365. }
  366. /*
  367.         Copyright 2003 JavaScript-coder.com. All rights reserved.
  368. */
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: