commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rlel...@apache.org
Subject cvs commit: jakarta-commons/validator/src/javascript/org/apache/commons/validator/javascript validateEmail.js
Date Sun, 18 May 2003 21:36:21 GMT
rleland     2003/05/18 14:36:21

  Added:       validator/src/javascript/org/apache/commons/validator/javascript
                        validateEmail.js
  Log:
  Add Javascript validations for Email
  
  Revision  Changes    Path
  1.1                  jakarta-commons/validator/src/javascript/org/apache/commons/validator/javascript/validateEmail.js
  
  Index: validateEmail.js
  ===================================================================
  
      function validateEmail(form) {
          var bValid = true;
          var focusField = null;
          var i = 0;
          var fields = new Array();
          oEmail = new email();
          for (x in oEmail) {
              if ((form[oEmail[x][0]].type == 'text' ||
                   form[oEmail[x][0]].type == 'textarea') &&
                  (form[oEmail[x][0]].value.length > 0)) {
                  if (!checkEmail(form[oEmail[x][0]].value)) {
                      if (i == 0) {
                          focusField = form[oEmail[x][0]];
                      }
                      fields[i++] = oEmail[x][1];
                      bValid = false;
                  }
              }
          }
          if (fields.length > 0) {
              focusField.focus();
              alert(fields.join('\n'));
          }
          return bValid;
      }
  
      /**
       * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
       * http://javascript.internet.com
       */
      function checkEmail(emailStr) {
         if (emailStr.length == 0) {
             return true;
         }
         var emailPat=/^(.+)@(.+)$/;
         var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
         var validChars="\[^\\s" + specialChars + "\]";
         var quotedUser="(\"[^\"]*\")";
         var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
         var atom=validChars + '+';
         var word="(" + atom + "|" + quotedUser + ")";
         var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
         var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
         var matchArray=emailStr.match(emailPat);
         if (matchArray == null) {
             return false;
         }
         var user=matchArray[1];
         var domain=matchArray[2];
         if (user.match(userPat) == null) {
             return false;
         }
         var IPArray = domain.match(ipDomainPat);
         if (IPArray != null) {
             for (var i = 1; i <= 4; i++) {
                if (IPArray[i] > 255) {
                   return false;
                }
             }
             return true;
         }
         var domainArray=domain.match(domainPat);
         if (domainArray == null) {
             return false;
         }
         var atomPat=new RegExp(atom,"g");
         var domArr=domain.match(atomPat);
         var len=domArr.length;
         if ((domArr[domArr.length-1].length < 2) ||
             (domArr[domArr.length-1].length > 3)) {
             return false;
         }
         if (len < 2) {
             return false;
         }
         return true;
      }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message