home *** CD-ROM | disk | FTP | other *** search
/ 66.142.0.142 / 66.142.0.142.tar / 66.142.0.142 / js / ewvalidator.js < prev    next >
Text File  |  2009-07-15  |  12KB  |  408 lines

  1. // Check US Date format (mm/dd/yyyy)
  2. function ew_CheckUSDate(object_value) {
  3.     if (object_value == null)
  4.         return true;
  5.     if (object_value.length == 0)
  6.         return true;
  7.     isplit = object_value.indexOf(EW_DATE_SEPARATOR); // Split by date separator
  8.     if (isplit == -1 || isplit == object_value.length)
  9.         return false;
  10.     sMonth = object_value.substring(0, isplit);
  11.     if (sMonth.length == 0)
  12.         return false;
  13.     isplit = object_value.indexOf(EW_DATE_SEPARATOR, isplit + 1); // Split by date separator
  14.     if (isplit == -1 || (isplit + 1 ) == object_value.length)
  15.         return false;
  16.     sDay = object_value.substring((sMonth.length + 1), isplit);
  17.     if (sDay.length == 0)
  18.         return false;
  19.     isep = object_value.indexOf(' ', isplit + 1); 
  20.     if (isep == -1) {
  21.         sYear = object_value.substring(isplit + 1);
  22.     } else {
  23.         sYear = object_value.substring(isplit + 1, isep);
  24.         sTime = object_value.substring(isep + 1);
  25.         if (!ew_CheckTime(sTime)) // Check time
  26.             return false; 
  27.     }
  28.     if (!ew_CheckInteger(sMonth))
  29.         return false;
  30.     else if (!ew_CheckRange(sMonth, 1, 12))
  31.         return false;
  32.     else if (!ew_CheckInteger(sYear))
  33.         return false;
  34.     else if (!ew_CheckRange(sYear, 0, 9999))
  35.         return false;
  36.     else if (!ew_CheckInteger(sDay))
  37.         return false;
  38.     else if (!ew_CheckDay(sYear, sMonth, sDay))
  39.         return false;
  40.     else
  41.         return true;
  42. }
  43.  
  44. // Check Date format (yyyy/mm/dd)
  45. function ew_CheckDate(object_value) {
  46.     if (object_value == null)
  47.         return true;
  48.     if (object_value.length == 0)
  49.         return true;
  50.     isplit = object_value.indexOf(EW_DATE_SEPARATOR); // Split by date separator
  51.     if (isplit == -1 || isplit == object_value.length)
  52.         return false;
  53.     sYear = object_value.substring(0, isplit);
  54.     isplit = object_value.indexOf(EW_DATE_SEPARATOR, isplit + 1); // Split by date separator
  55.     if (isplit == -1 || (isplit + 1 ) == object_value.length)
  56.         return false;
  57.     sMonth = object_value.substring((sYear.length + 1), isplit);
  58.     if (sMonth.length == 0)
  59.         return false;
  60.     isep = object_value.indexOf(' ', isplit + 1); 
  61.     if (isep == -1) {
  62.         sDay = object_value.substring(isplit + 1);
  63.     } else {
  64.         sDay = object_value.substring(isplit + 1, isep);
  65.         sTime = object_value.substring(isep + 1);
  66.         if (!ew_CheckTime(sTime)) // Check time
  67.             return false; 
  68.     }
  69.     if (sDay.length == 0)
  70.         return false;
  71.     if (!ew_CheckInteger(sMonth))
  72.         return false;
  73.     else if (!ew_CheckRange(sMonth, 1, 12))
  74.         return false;
  75.     else if (!ew_CheckInteger(sYear))
  76.         return false;
  77.     else if (!ew_CheckRange(sYear, 0, 9999))
  78.         return false;
  79.     else if (!ew_CheckInteger(sDay))
  80.         return false;
  81.     else if (!ew_CheckDay(sYear, sMonth, sDay))
  82.         return false;
  83.     else
  84.         return true;
  85. }
  86.  
  87. // Check Euro Date format (dd/mm/yyyy)
  88. function ew_CheckEuroDate(object_value) {
  89.     if (object_value == null)
  90.         return true;
  91.     if (object_value.length == 0)
  92.       return true;
  93.     isplit = object_value.indexOf(EW_DATE_SEPARATOR); // Split by date separator
  94.     if (isplit == -1 || isplit == object_value.length)
  95.         return false;
  96.     sDay = object_value.substring(0, isplit);
  97.     monthSplit = isplit + 1;
  98.     isplit = object_value.indexOf(EW_DATE_SEPARATOR, monthSplit); // Split by date separator
  99.     if (isplit == -1 ||  (isplit + 1 )  == object_value.length)
  100.         return false;
  101.     sMonth = object_value.substring((sDay.length + 1), isplit);
  102.     isep = object_value.indexOf(' ', isplit + 1); 
  103.     if (isep == -1) {
  104.         sYear = object_value.substring(isplit + 1);
  105.     } else {
  106.         sYear = object_value.substring(isplit + 1, isep);
  107.         sTime = object_value.substring(isep + 1);
  108.         if (!ew_CheckTime(sTime))
  109.             return false; 
  110.     }
  111.     if (!ew_CheckInteger(sMonth))
  112.         return false;
  113.     else if (!ew_CheckRange(sMonth, 1, 12))
  114.         return false;
  115.     else if (!ew_CheckInteger(sYear))
  116.         return false;
  117.     else if (!ew_CheckRange(sYear, 0, null))
  118.         return false;
  119.     else if (!ew_CheckInteger(sDay))
  120.         return false;
  121.     else if (!ew_CheckDay(sYear, sMonth, sDay))
  122.         return false;
  123.     else
  124.         return true;
  125. }
  126.  
  127. // Check day
  128. function ew_CheckDay(checkYear, checkMonth, checkDay) {
  129.     maxDay = 31;
  130.     if (checkMonth == 4 || checkMonth == 6 ||    checkMonth == 9 || checkMonth == 11) {
  131.         maxDay = 30;
  132.     } else if (checkMonth == 2)    {
  133.         if (checkYear % 4 > 0)
  134.             maxDay =28;
  135.         else if (checkYear % 100 == 0 && checkYear % 400 > 0)
  136.             maxDay = 28;
  137.         else
  138.             maxDay = 29;
  139.     }
  140.     return ew_CheckRange(checkDay, 1, maxDay);
  141. }
  142.  
  143. // Check integer
  144. function ew_CheckInteger(object_value) {
  145.     if (object_value == null)
  146.         return true;
  147.     if (object_value.length == 0)
  148.         return true;
  149.     var decimal_format = ".";
  150.     var check_char;
  151.     check_char = object_value.indexOf(decimal_format);
  152.     if (check_char < 1)
  153.         return ew_CheckNumber(object_value);
  154.     else
  155.         return false;
  156. }
  157.  
  158. // Check number range
  159. function ew_NumberRange(object_value, min_value, max_value) {
  160.     if (min_value != null) {
  161.         if (object_value < min_value)
  162.             return false;
  163.     }
  164.     if (max_value != null) {
  165.         if (object_value > max_value)
  166.             return false;
  167.     }
  168.     return true;
  169. }
  170.  
  171. // Check number
  172. function ew_CheckNumber(object_value) {
  173.     if (object_value == null)
  174.         return true;
  175.     if (object_value.length == 0)
  176.         return true;
  177.     var start_format = " .+-0123456789";
  178.     var number_format = " .0123456789";
  179.     var check_char;
  180.     var decimal = false;
  181.     var trailing_blank = false;
  182.     var digits = false;
  183.     check_char = start_format.indexOf(object_value.charAt(0));
  184.     if (check_char == 1)
  185.         decimal = true;
  186.     else if (check_char < 1)
  187.         return false;
  188.     for (var i = 1; i < object_value.length; i++)    {
  189.         check_char = number_format.indexOf(object_value.charAt(i))
  190.         if (check_char < 0) {
  191.             return false;
  192.         } else if (check_char == 1)    {
  193.             if (decimal)
  194.                 return false;
  195.             else
  196.                 decimal = true;
  197.         } else if (check_char == 0) {
  198.             if (decimal || digits)    
  199.             trailing_blank = true;
  200.         }    else if (trailing_blank) { 
  201.             return false;
  202.         } else {
  203.             digits = true;
  204.         }
  205.     }    
  206.     return true;
  207. }
  208.  
  209. // Check range
  210. function ew_CheckRange(object_value, min_value, max_value) {
  211.     if (object_value == null)
  212.         return true;
  213.     if (object_value.length == 0)
  214.         return true;
  215.     if (!ew_CheckNumber(object_value))
  216.         return false;
  217.     else
  218.         return (ew_NumberRange((eval(object_value)), min_value, max_value));
  219.     return true;
  220. }
  221.  
  222. // Check time
  223. function ew_CheckTime(object_value) {
  224.     if (object_value == null)
  225.         return true;
  226.     if (object_value.length == 0)
  227.         return true;
  228.     isplit = object_value.indexOf(':');
  229.     if (isplit == -1 || isplit == object_value.length)
  230.         return false;
  231.     sHour = object_value.substring(0, isplit);
  232.     iminute = object_value.indexOf(':', isplit + 1);
  233.     if (iminute == -1 || iminute == object_value.length)
  234.         sMin = object_value.substring((sHour.length + 1));
  235.     else
  236.         sMin = object_value.substring((sHour.length + 1), iminute);
  237.     if (!ew_CheckInteger(sHour))
  238.         return false;
  239.     else if (!ew_CheckRange(sHour, 0, 23))
  240.         return false;
  241.     if (!ew_CheckInteger(sMin))
  242.         return false;
  243.     else if (!ew_CheckRange(sMin, 0, 59))
  244.         return false;
  245.     if (iminute != -1) {
  246.         sSec = object_value.substring(iminute + 1);        
  247.         if (!ew_CheckInteger(sSec))
  248.             return false;
  249.         else if (!ew_CheckRange(sSec, 0, 59))
  250.             return false;    
  251.     }
  252.     return true;
  253. }
  254.  
  255. // Check phone
  256. function ew_CheckPhone(object_value) {
  257.     if (object_value == null)
  258.         return true;
  259.     if (object_value.length == 0)
  260.         return true;
  261.     if (object_value.length != 12)
  262.         return false;
  263.     if (!ew_CheckNumber(object_value.substring(0,3)))
  264.         return false;
  265.     else if (!ew_NumberRange((eval(object_value.substring(0,3))), 100, 1000))
  266.         return false;
  267.     if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ")
  268.         return false
  269.     if (!ew_CheckNumber(object_value.substring(4,7)))
  270.         return false;
  271.     else if (!ew_NumberRange((eval(object_value.substring(4,7))), 100, 1000))
  272.         return false;
  273.     if (object_value.charAt(7) != "-" && object_value.charAt(7) != " ")
  274.         return false;
  275.     if (object_value.charAt(8) == "-" || object_value.charAt(8) == "+")
  276.         return false;
  277.     else
  278.         return (ew_CheckInteger(object_value.substring(8,12)));
  279. }
  280.  
  281. // Check zip
  282. function ew_CheckZip(object_value) {
  283.     if (object_value == null)
  284.         return true;
  285.     if (object_value.length == 0)
  286.         return true;
  287.     if (object_value.length != 5 && object_value.length != 10)
  288.         return false;
  289.     if (object_value.charAt(0) == "-" || object_value.charAt(0) == "+")
  290.         return false;
  291.     if (!ew_CheckInteger(object_value.substring(0,5)))
  292.         return false;
  293.     if (object_value.length == 5)
  294.         return true;
  295.     if (object_value.charAt(5) != "-" && object_value.charAt(5) != " ")
  296.         return false;
  297.     if (object_value.charAt(6) == "-" || object_value.charAt(6) == "+")
  298.         return false;
  299.     return (ew_CheckInteger(object_value.substring(6,10)));
  300. }
  301.  
  302. // Check credit card
  303. function ew_CheckCreditCard(object_value) {
  304.     var white_space = " -";
  305.     var creditcard_string = "";
  306.     var check_char;
  307.     if (object_value == null)
  308.         return true;
  309.     if (object_value.length == 0)
  310.         return true;
  311.     for (var i = 0; i < object_value.length; i++) {
  312.         check_char = white_space.indexOf(object_value.charAt(i));
  313.         if (check_char < 0)
  314.             creditcard_string += object_value.substring(i, (i + 1));
  315.     }    
  316.     if (creditcard_string.length == 0)
  317.         return false;     
  318.     if (creditcard_string.charAt(0) == "+")
  319.         return false;
  320.     if (!ew_CheckInteger(creditcard_string))
  321.         return false;
  322.     var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
  323.     var checkdigit = 0;
  324.     var tempdigit;
  325.     for (var i = 0; i < creditcard_string.length; i++) {
  326.         tempdigit = eval(creditcard_string.charAt(i));        
  327.         if (doubledigit) {
  328.             tempdigit *= 2;
  329.             checkdigit += (tempdigit % 10);            
  330.             if ((tempdigit / 10) >= 1.0)
  331.                 checkdigit++;            
  332.             doubledigit = false;
  333.         }    else {
  334.             checkdigit += tempdigit;
  335.             doubledigit = true;
  336.         }
  337.     }
  338.     return (checkdigit % 10) == 0 ? true : false;
  339. }
  340.  
  341. // Check social security number
  342. function ew_CheckSSC(object_value) {
  343.     var white_space = " -+.";
  344.     var ssc_string="";
  345.     var check_char;
  346.     if (object_value == null)
  347.         return true;
  348.     if (object_value.length == 0)
  349.         return true;
  350.     if (object_value.length != 11)
  351.         return false;
  352.     if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ")
  353.         return false;
  354.     if (object_value.charAt(6) != "-" && object_value.charAt(6) != " ")
  355.         return false;
  356.     for (var i = 0; i < object_value.length; i++) {
  357.         check_char = white_space.indexOf(object_value.charAt(i));
  358.         if (check_char < 0)
  359.             ssc_string += object_value.substring(i, (i + 1));
  360.     }    
  361.     if (ssc_string.length != 9)
  362.         return false;     
  363.     if (!ew_CheckInteger(ssc_string))
  364.         return false;
  365.     return true;
  366. }
  367.  
  368. // Check email
  369. function ew_CheckEmail(object_value) {
  370.     if (object_value == null)
  371.         return true;
  372.     if (object_value.length == 0)
  373.         return true;
  374.     if (!(object_value.indexOf("@") > -1 && object_value.indexOf(".") > -1))
  375.         return false;    
  376.     return true;
  377. }
  378.  
  379. // Check GUID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}    
  380. function ew_CheckGUID(object_value) {
  381.     if (object_value == null)
  382.         return true;
  383.     if (object_value.length == 0)
  384.         return true;
  385.     object_value = object_value.replace(/^\s*|\s*$/g, "");
  386.     var re = new RegExp("^(\{{1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{1})$");
  387.     return re.test(object_value);
  388. }
  389.  
  390. // Check file extension
  391. function ew_CheckFileType(object_value) {
  392.     if (object_value == null)
  393.         return true;
  394.     if (object_value.length == 0)
  395.         return true;
  396.     if (typeof EW_UPLOAD_ALLOWED_FILE_EXT == "undefined")
  397.         return true;
  398.     if (EW_UPLOAD_ALLOWED_FILE_EXT == "")
  399.         return true;
  400.     var fileTypes = EW_UPLOAD_ALLOWED_FILE_EXT.split(",");
  401.     var ext = object_value.substring(object_value.lastIndexOf(".")+1, object_value.length).toLowerCase(); 
  402.     for (var i=0; i < fileTypes.length; i++) { 
  403.         if (fileTypes[i] == ext) 
  404.             return true; 
  405.     } 
  406.     return false; 
  407. }
  408.