SHARE
TWEET

Untitled

a guest Jul 17th, 2017 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function checkEmail (e) {
  2.    
  3.     var i, j, l = e.length;
  4.     var foundPoint = false;
  5.  
  6.     function checkChars (s, i, l) {
  7.         while (i < l && ("_-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789").indexOf(s.charAt(i)) != -1){
  8.             i++;
  9.         }
  10.         return i;
  11.     }
  12.     function checkFirstLevelDomainChars (s, i, l) {
  13.         while (i < l && ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").indexOf(s.charAt(j)) != -1) {
  14.             i++;
  15.         }
  16.         return (i == l);
  17.     }
  18.     trace(e);
  19.  
  20.     // every email starts with a string
  21.     if ((i=checkChars(e, 0, l)) == 0) {
  22.         return -1;
  23.     }
  24.     //init j
  25.     j=i;
  26.  
  27.     // followed by an arbitrary number of ("." string) combinations
  28.     while (i < l && e.charAt(i) == ".") {
  29.         // skip the point
  30.         i++;
  31.         // if there are no chars, we have an error
  32.         if ((j=checkChars(e, i, l)) == i) {
  33.             return -2;
  34.         }
  35.         // else skip the chars
  36.         i = j;
  37.     }
  38.     // then follows the magic @
  39.     if (e.charAt(i) != "@"){
  40.         return -3;
  41.     }
  42.  
  43.     // followed by minimum one string point string
  44.     // after the last point minimum 2 characters are allowed
  45.  
  46.     do {
  47.         // skip the @ (j == i at the beginning, so it is like i++)
  48.         i = j+1;
  49.         // do we have more chars ?
  50.         j = checkChars(e, i, l);
  51.         if (j == i) {
  52.             // no more chars found -> error
  53.             return -4;
  54.         } else if (j == e.length) {
  55.             // emailaddress is finished, do we have a first level domain ?
  56.             j -= i;
  57.             // we have one if it is at least 2 long and consists of the correct characters
  58.             if(foundPoint && j>=2 && checkFirstLevelDomainChars(e, i, l)){
  59.                 return 1
  60.             } else {
  61.                 return -5
  62.             }
  63.         }
  64.         // if we reach the end or don't have a point, we return an error
  65.         foundPoint = (e.charAt(j) == ".");
  66.     } while (i < l && foundPoint);
  67.     return -6;
  68. }
RAW Paste Data
Top