home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / ScriptBuilder / NOSB30_TRIAL.exe / data1.cab / Program_Files / CompLib / dataValidation.js < prev    next >
Encoding:
Text File  |  1998-10-05  |  18.9 KB  |  546 lines

  1.  function com_netobjects_dataValidation( params ) {
  2.     this.isAlpha = isAlpha;
  3.     this.isAlphaNum = isAlphaNum;
  4.     this.isAlphaNumOrUnderscore = isAlphaNumOrUnderscore;
  5.     this.isBlank = isBlank;
  6.     this.isInt = isInt;
  7.     this.isNull = isNull;
  8.     this.isNum = isNum;
  9.     this.isUndef = isUndef;
  10.     this.isValid5DigitZip = isValid5DigitZip;
  11.     this.isValid5Plus4DigitZip = isValid5Plus4DigitZip;
  12.     this.isValidEmail = isValidEmail;
  13.     this.isValidPhone = isValidPhone;
  14.     this.isValidSSN = isValidSSN;
  15.  
  16.     this.name = (params.name+"" != "undefined" ? params.name : "dataValidation1");
  17.     
  18.     /* ======================================================================
  19.     FUNCTION:      isAlpha
  20.     
  21.     INPUT:        str (string) - the string to be tested
  22.     
  23.     RETURN:      true, if the string contains only alphabetic characters 
  24.                     false, otherwise.
  25.     
  26.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  27.                       Microsoft Internet Explorer 3.02 and higher,
  28.                       Netscape Enterprise Server 3.0,
  29.                       Microsoft IIS/ASP 3.0.
  30.     ====================================================================== */
  31.     function isAlpha( str ) {
  32.         // Return immediately if an invalid value was passed in
  33.         if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  34.             return false;
  35.     
  36.         var isValid = true;
  37.     
  38.         str += "";    // convert to a string for performing string comparisons.
  39.     
  40.         // Loop through string one character at time,  breaking out of for
  41.         // loop when an non Alpha character is found.
  42.           for (i = 0; i < str.length; i++) {
  43.             // Alpha must be between "A"-"Z", or "a"-"z"
  44.             if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
  45.                       ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) ) {
  46.                              isValid = false;
  47.                              break;
  48.                       }
  49.        } // end for loop
  50.        
  51.         return isValid;
  52.     }  // end isAlpha 
  53.     
  54.     /* ======================================================================
  55.     FUNCTION:    isAlphaNum
  56.      
  57.     INPUT:        str (string) - a string that will be tested to ensure that
  58.                                           each character is a digit or a letter.
  59.     
  60.     RETURN:      true, if all characters in the string are a character from 0-9
  61.                      or a-z or A-Z;  
  62.                     false, otherwise
  63.     
  64.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  65.                       Microsoft Internet Explorer 3.02 and higher,
  66.                       Netscape Enterprise Server 3.0,
  67.                       Microsoft IIS/ASP 3.0.
  68.     ====================================================================== */
  69.     function isAlphaNum( str ) {
  70.         // Return immediately if an invalid value was passed in
  71.         if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  72.             return false;
  73.     
  74.         var isValid = true;
  75.         
  76.         // convert to a string for performing string comparisons.
  77.            str += "";    
  78.     
  79.         // Loop through length of string and test for any alpha numeric 
  80.         // characters
  81.            for (i = 0; i < str.length; i++)
  82.            {
  83.                 // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
  84.               if (!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
  85.                       ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
  86.                       ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z"))))
  87.                 {
  88.                     isValid = false;
  89.                     break;
  90.                 }    
  91.            } // END for   
  92.        
  93.            return isValid;
  94.     }  // end isAlphaNum
  95.     
  96.     /* ======================================================================
  97.     FUNCTION:    isAlphaNumOrUnderscore
  98.     
  99.     INPUT:        str (string) - the string to be tested
  100.     
  101.     RETURN:      true, if the string contains only alphanumeric characters or underscores.
  102.                     false, otherwise.
  103.     
  104.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  105.                       Microsoft Internet Explorer 3.02 and higher,
  106.                       Netscape Enterprise Server 3.0,
  107.                       Microsoft IIS/ASP 3.0.
  108.     ====================================================================== */
  109.     function isAlphaNumOrUnderscore( str ) {
  110.         // Return immediately if an invalid value was passed in
  111.         if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  112.             return false;
  113.     
  114.         var isValid = true;
  115.     
  116.         str += "";    // convert to a string for performing string comparisons.
  117.         // Loop through string one character at a time. If non-alpha numeric
  118.         // is found then, break out of loop and return a false result
  119.     
  120.         for (i = 0; i < str.length; i++)
  121.            {
  122.             // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
  123.                   if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
  124.                       ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
  125.                       ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
  126.                       (str.charAt(i) == "_") ) )
  127.                   {
  128.                        isValid = false;
  129.                      break;
  130.                   }
  131.     
  132.         } // END for   
  133.        
  134.         return isValid;
  135.     
  136.     }  // end isAlphaNumOrUnderscore
  137.     
  138.     /* ======================================================================
  139.     FUNCTION:    isBlank
  140.      
  141.     INPUT:        val - the value to be tested
  142.     
  143.     RETURN:      true, if the string is null, undefined or an empty string, ""
  144.                   false, otherwise.
  145.     
  146.     CALLS:        isNull(), isUndef() which are defined elsewhere in the Script Library
  147.     
  148.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  149.                       Microsoft Internet Explorer 3.02 and higher,
  150.                       Netscape Enterprise Server 3.0,
  151.                       Microsoft IIS/ASP 3.0.
  152.     ====================================================================== */
  153.     function isBlank( str ) {
  154.         var isValid = false;
  155.     
  156.          if ( isNull(str) || isUndef(str) || (str+"" == "") )
  157.              isValid = true;
  158.             
  159.         return isValid;
  160.     }  // end isBlank
  161.     
  162.     /* ======================================================================
  163.     FUNCTION:      isInt
  164.      
  165.     INPUT:          numstr (string/number)      - the string that will be tested to ensure 
  166.                                                      that each character is a digit
  167.                     allowNegatives (boolean) - (optional) when true, allows numstr to be
  168.                                                         negative (contain a '-').  When false,
  169.                                                       any negative number or a string starting
  170.                                                         with a '-' will be considered invalid.
  171.     
  172.     RETURN:      true, if all characters in the string are a character from 0-9,
  173.                     regardless of value for allowNegatives
  174.                     true, if allowNegatives is true and the string starts with a '-', and all other
  175.                     characters are 0-9.
  176.                      false, otherwise.
  177.     
  178.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  179.                       Microsoft Internet Explorer 3.02 and higher,
  180.                       Netscape Enterprise Server 3.0,
  181.                       Microsoft IIS/ASP 3.0.
  182.     ====================================================================== */
  183.     function isInt( numstr, allowNegatives ) {
  184.         // Return immediately if an invalid value was passed in
  185.         if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")    
  186.             return false;
  187.     
  188.         // Default allowNegatives to true when undefined or null
  189.         if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null")    
  190.             allowNegatives = true;
  191.     
  192.         var isValid = true;
  193.     
  194.         // convert to a string for performing string comparisons.
  195.         numstr += "";    
  196.     
  197.         // Loop through string and test each character. If any
  198.         // character is not a number, return a false result.
  199.          // Include special case for negative numbers (first char == '-').   
  200.         for (i = 0; i < numstr.length; i++) {
  201.             if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
  202.                isValid = false;
  203.                break;
  204.             } else if ((numstr.charAt(i) == "-" && i != 0) || 
  205.                     (numstr.charAt(i) == "-" && !allowNegatives)) {
  206.                isValid = false;
  207.                break;
  208.           }
  209.                                      
  210.        } // END for   
  211.        
  212.            return isValid;
  213.     }  // end isInt
  214.     
  215.     /* ======================================================================
  216.     FUNCTION:    isNull
  217.      
  218.     INPUT:        val - the value to be tested
  219.     
  220.     RETURN:      true, if the value is null;
  221.                   false, otherwise.
  222.     
  223.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  224.                       Microsoft Internet Explorer 3.02 and higher,
  225.                       Netscape Enterprise Server 3.0,
  226.                       Microsoft IIS/ASP 3.0.
  227.     ====================================================================== */
  228.     function isNull( val ) {
  229.         var isValid = false;
  230.     
  231.          if (val+"" == "null")
  232.              isValid = true;
  233.             
  234.         return isValid;
  235.     }  // end isNull
  236.     
  237.     /* ======================================================================
  238.     FUNCTION:      isNum
  239.      
  240.     INPUT:          numstr (string/number) - the string that will be tested to ensure 
  241.                                                    that the value is a number (int or float)
  242.     
  243.     RETURN:      true, if all characters represent a valid integer or float
  244.                      false, otherwise.
  245.     
  246.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  247.                       Microsoft Internet Explorer 3.02 and higher,
  248.                       Netscape Enterprise Server 3.0,
  249.                       Microsoft IIS/ASP 3.0.
  250.     ====================================================================== */
  251.     function isNum( numstr ) {
  252.         // Return immediately if an invalid value was passed in
  253.         if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")    
  254.             return false;
  255.     
  256.         var isValid = true;
  257.         var decCount = 0;        // number of decimal points in the string
  258.     
  259.         // convert to a string for performing string comparisons.
  260.         numstr += "";    
  261.     
  262.         // Loop through string and test each character. If any
  263.         // character is not a number, return a false result.
  264.          // Include special cases for negative numbers (first char == '-')
  265.         // and a single decimal point (any one char in string == '.').   
  266.         for (i = 0; i < numstr.length; i++) {
  267.             // track number of decimal points
  268.             if (numstr.charAt(i) == ".")
  269.                 decCount++;
  270.     
  271.             if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
  272.                     (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
  273.                isValid = false;
  274.                break;
  275.             } else if ((numstr.charAt(i) == "-" && i != 0) ||
  276.                     (numstr.charAt(i) == "." && numstr.length == 1) ||
  277.                   (numstr.charAt(i) == "." && decCount > 1)) {
  278.                isValid = false;
  279.                break;
  280.           }                                 
  281.     //if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
  282.        } // END for   
  283.        
  284.            return isValid;
  285.     }  // end isNum
  286.     
  287.     /* ======================================================================
  288.     FUNCTION:    isUndef
  289.      
  290.     INPUT:        val - the value to be tested
  291.     
  292.     RETURN:      true, if the value is undefined
  293.                   false, otherwise.
  294.     
  295.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  296.                       Microsoft Internet Explorer 3.02 and higher,
  297.                       Netscape Enterprise Server 3.0,
  298.                       Microsoft IIS/ASP 3.0.
  299.     ====================================================================== */
  300.     function isUndef( val ) {
  301.         var isValid = false;
  302.     
  303.          if (val+"" == "undefined")
  304.              isValid = true;
  305.             
  306.         return isValid;
  307.     }  // end isUndef
  308.     
  309.     /* ======================================================================
  310.     FUNCTION:      isValid5DigitZip
  311.      
  312.     INPUT:        str (string) - a 5-digit zip code to be tested
  313.     
  314.     RETURN:      true, if the string is 5-digits long
  315.                     false, otherwise
  316.     
  317.     CALLS:        isBlank(), isInt() which are defined elsewhere in the Script Library
  318.     
  319.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  320.                       Microsoft Internet Explorer 3.02 and higher,
  321.                       Netscape Enterprise Server 3.0,
  322.                       Microsoft IIS/ASP 3.0.
  323.     ====================================================================== */
  324.     function isValid5DigitZip( str ) {
  325.         // Return immediately if an invalid value was passed in
  326.         if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  327.             return false;
  328.         
  329.         var isValid = true;
  330.     
  331.         str += "";
  332.     
  333.         // Rules: zipstr must be 5 characters long, and can only contain numbers from
  334.        // 0 through 9
  335.        if (isBlank(str) || (str.length != 5) || !isInt(str, false))
  336.             isValid = false;
  337.        
  338.        return isValid;
  339.     } // end isValid5DigitZip
  340.     
  341.     /* ======================================================================
  342.     FUNCTION:      isValid5Plus4DigitZip
  343.      
  344.     INPUT:        str (string) - a 5+4 digit zip code to be tested
  345.     
  346.     RETURN:      true, if the string contains 5-digits followed by a dash followed by 4 digits
  347.                     false, otherwise
  348.     
  349.     CALLS:        isBlank(), isInt() which are defined elsewhere in the Script Library
  350.     
  351.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  352.                       Microsoft Internet Explorer 3.02 and higher,
  353.                       Netscape Enterprise Server 3.0,
  354.                       Microsoft IIS/ASP 3.0.
  355.     ====================================================================== */
  356.     function isValid5Plus4DigitZip( str ) {
  357.         // Return immediately if an invalid value was passed in
  358.         if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  359.             return false;
  360.     
  361.         var isValid = true;
  362.     
  363.         str += "";
  364.     
  365.         // Rules: The first five characters may contain only digits 0-9,
  366.         // the 6th character must be a dash, '-', and 
  367.         // the last four characters may contain only digits 0-9.
  368.         // The total string length must be 10 characters.
  369.            if (isBlank(str) || (str.length != 10) || 
  370.                 !isInt(str.substring(0,5), false) || str.charAt(5) != '-' ||
  371.                 !isInt(str.substring(6,10), false))
  372.             isValid = false;
  373.        
  374.            return isValid;
  375.     } // end isValid5Plus4DigitZip
  376.     
  377.     /* ======================================================================
  378.     FUNCTION:      isValidEmail
  379.      
  380.     INPUT:        str (string) - an e-mail address to be tested
  381.     
  382.     RETURN:      true, if the string contains a valid e-mail address which is a string
  383.                     plus an '@' character followed by another string containing at least 
  384.                     one '.' and ending in an alpha (non-punctuation) character.
  385.                     false, otherwise
  386.     
  387.     CALLS:        isBlank(), isAlpha() which are defined elsewhere in the Script Library
  388.     
  389.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  390.                       Microsoft Internet Explorer 3.02 and higher,
  391.                       Netscape Enterprise Server 3.0,
  392.                       Microsoft IIS/ASP 3.0.
  393.     ====================================================================== */
  394.     function isValidEmail( str ) {
  395.         // Return immediately if an invalid value was passed in
  396.         if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  397.             return false;
  398.     
  399.         var isValid = true;
  400.     
  401.         str += "";
  402.     
  403.         namestr = str.substring(0, str.indexOf("@"));  // everything before the '@'
  404.         domainstr = str.substring(str.indexOf("@")+1, str.length); // everything after the '@'
  405.     
  406.         // Rules: namestr cannot be empty, or that would indicate no characters before the '@',
  407.         // domainstr must contain a period that is not the first character (i.e. right after
  408.         // the '@').  The last character must be an alpha.
  409.            if (isBlank(str) || (namestr.length == 0) || 
  410.                 (domainstr.indexOf(".") <= 0) ||
  411.                 (domainstr.indexOf("@") != -1) ||
  412.                 !isAlpha(str.charAt(str.length-1)))
  413.             isValid = false;
  414.        
  415.            return isValid;
  416.     } // end isValidEmail
  417.     
  418.     /* ======================================================================
  419.     FUNCTION:      isValidPhone
  420.      
  421.     INPUT:        str (string) - an phone number to be tested
  422.                     incAreaCode (boolean) - if true, area code is included (10-digits);
  423.                                                     if false or undefined, area code not included
  424.     
  425.     RETURN:      true, if the string contains a 7-digit phone number and incAreaCode == false
  426.                     or is undefined
  427.                     true, if the string contains a 10-digit phone number and incAreaCode == true
  428.                     false, otherwise
  429.     
  430.     CALLS:        StripNonNumeric(), which is defined elsewhere in the Script Library
  431.     
  432.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  433.                       Microsoft Internet Explorer 3.02 and higher,
  434.                       Netscape Enterprise Server 3.0,
  435.                       Microsoft IIS/ASP 3.0.
  436.     ====================================================================== */
  437.     function isValidPhone( str, incAreaCode ) {
  438.         // Return immediately if an invalid value was passed in
  439.         if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  440.             return false;
  441.     
  442.         // Set default value for incAreaCode to false, if undefined or null
  443.         if (incAreaCode+"" == "undefined" || incAreaCode+"" == "null")    
  444.             incAreaCode = false;
  445.     
  446.         var isValid = true;
  447.     
  448.         str += "";
  449.     
  450.         // After stripping out non-numeric characters, such as dashes, the
  451.         // phone number should contain 7 digits (no area code) or 10 digits (area code)
  452.         str = StripNonNumeric(str+"");
  453.         if (incAreaCode && str.length != 10)
  454.             isValid = false;
  455.        if (!incAreaCode && str.length != 7)
  456.             isValid = false;
  457.     
  458.            return isValid;
  459.     } // end isValidPhone
  460.     
  461.     /* ======================================================================
  462.     FUNCTION:      isValidSSN
  463.      
  464.     INPUT:        str (string) - an phone number to be tested
  465.                     incDashes (boolean) - if true, str includes dashes (e.g. 111-12-3456);
  466.                                                  if false, str contains only digits
  467.                     
  468.     RETURN:      true, if the string contains digits and dashes in the form 111-12-3456;
  469.                     true, if the string contains a 9-digit number and incDashes is false;
  470.                     false, otherwise
  471.     
  472.     CALLS:        isInt(), which is defined elsewhere in the Script Library
  473.     
  474.     PLATFORMS:    Netscape Navigator 3.01 and higher,
  475.                       Microsoft Internet Explorer 3.02 and higher,
  476.                       Netscape Enterprise Server 3.0,
  477.                       Microsoft IIS/ASP 3.0.
  478.     ====================================================================== */
  479.     function isValidSSN( str, incDashes ) {
  480.         // Return immediately if an invalid value was passed in
  481.         if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  482.             return false;
  483.     
  484.         var isValid = true;
  485.     
  486.         // Set default value for incDashes to true, if undefined or null
  487.         if (incDashes+"" == "undefined" || incDashes+"" == "null")    
  488.             incDashes = true;
  489.     
  490.         str += "";    // make sure it's a string
  491.     
  492.         if (!incDashes && (!isNum(str) || str.length != 9))
  493.             isValid = false;
  494.     
  495.         var part1 = str.substring(0,3);
  496.         var part2 = str.substring(4,6);
  497.         var part3 = str.substring(7,str.length);
  498.     
  499.         // Ensure that the first part is a number and 3 digits long,
  500.         // the second part is a number and 2 digits long,
  501.         // the third part is a number and 4 digits long, e.g. 111-22-3333
  502.         if (incDashes && ((!isInt(part1, false) || part1.length != 3) ||
  503.                 (!isInt(part2, false) || part2.length != 2) || 
  504.                 (!isInt(part3, false) || part3.length != 4)) )
  505.             isValid = false;
  506.     
  507.            return isValid;
  508.     } // end isValidSSN
  509.     
  510.     /* ======================================================================
  511. FUNCTION:    StripNonNumeric
  512.  
  513. INPUT:          str (string) - a string to be altered
  514.  
  515. RETURN:         a string containing only numeric characters 0-9;
  516.                 returns null if invalid arguments were passed
  517.  
  518. DESC:            This function removes all non-numeric characters from a given
  519.                 string.  It is useful for removing dashes, parentheses, etc. from input 
  520.                 strings such as credit card numbers or phone nubmers.
  521. ====================================================================== */
  522. function StripNonNumeric( str ) {
  523.     var     resultStr = "";
  524.  
  525.     // Return immediately if an invalid value was passed in
  526.     if (str+"" == "undefined" || str == null)    
  527.         return null;
  528.  
  529.     // Make sure the argument is a string
  530.     str += "";
  531.  
  532.     // Loop through entire string, adding each character from the original
  533.     // string if it is a number
  534.     for (var i=0; i < str.length; i++)
  535.     {
  536.        if ( (str.charAt(i) >= "0") && (str.charAt(i) <= "9") )
  537.                   resultStr = resultStr + str.charAt(i);
  538.  
  539.    } // end for loop      
  540.  
  541.    return resultStr;
  542. }  // end StripNonNumeric
  543.  
  544. }
  545.     
  546.