home *** CD-ROM | disk | FTP | other *** search
- function com_netobjects_dataValidation( params ) {
- this.isAlpha = isAlpha;
- this.isAlphaNum = isAlphaNum;
- this.isAlphaNumOrUnderscore = isAlphaNumOrUnderscore;
- this.isBlank = isBlank;
- this.isInt = isInt;
- this.isNull = isNull;
- this.isNum = isNum;
- this.isUndef = isUndef;
- this.isValid5DigitZip = isValid5DigitZip;
- this.isValid5Plus4DigitZip = isValid5Plus4DigitZip;
- this.isValidEmail = isValidEmail;
- this.isValidPhone = isValidPhone;
- this.isValidSSN = isValidSSN;
-
- this.name = (params.name+"" != "undefined" ? params.name : "dataValidation1");
-
- /* ======================================================================
- FUNCTION: isAlpha
-
- INPUT: str (string) - the string to be tested
-
- RETURN: true, if the string contains only alphabetic characters
- false, otherwise.
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isAlpha( str ) {
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
- return false;
-
- var isValid = true;
-
- str += ""; // convert to a string for performing string comparisons.
-
- // Loop through string one character at time, breaking out of for
- // loop when an non Alpha character is found.
- for (i = 0; i < str.length; i++) {
- // Alpha must be between "A"-"Z", or "a"-"z"
- if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
- ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) ) {
- isValid = false;
- break;
- }
- } // end for loop
-
- return isValid;
- } // end isAlpha
-
- /* ======================================================================
- FUNCTION: isAlphaNum
-
- INPUT: str (string) - a string that will be tested to ensure that
- each character is a digit or a letter.
-
- RETURN: true, if all characters in the string are a character from 0-9
- or a-z or A-Z;
- false, otherwise
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isAlphaNum( str ) {
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
- return false;
-
- var isValid = true;
-
- // convert to a string for performing string comparisons.
- str += "";
-
- // Loop through length of string and test for any alpha numeric
- // characters
- for (i = 0; i < str.length; i++)
- {
- // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
- if (!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) ||
- ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
- ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z"))))
- {
- isValid = false;
- break;
- }
- } // END for
-
- return isValid;
- } // end isAlphaNum
-
- /* ======================================================================
- FUNCTION: isAlphaNumOrUnderscore
-
- INPUT: str (string) - the string to be tested
-
- RETURN: true, if the string contains only alphanumeric characters or underscores.
- false, otherwise.
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isAlphaNumOrUnderscore( str ) {
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
- return false;
-
- var isValid = true;
-
- str += ""; // convert to a string for performing string comparisons.
- // Loop through string one character at a time. If non-alpha numeric
- // is found then, break out of loop and return a false result
-
- for (i = 0; i < str.length; i++)
- {
- // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
- if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) ||
- ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
- ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
- (str.charAt(i) == "_") ) )
- {
- isValid = false;
- break;
- }
-
- } // END for
-
- return isValid;
-
- } // end isAlphaNumOrUnderscore
-
- /* ======================================================================
- FUNCTION: isBlank
-
- INPUT: val - the value to be tested
-
- RETURN: true, if the string is null, undefined or an empty string, ""
- false, otherwise.
-
- CALLS: isNull(), isUndef() which are defined elsewhere in the Script Library
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isBlank( str ) {
- var isValid = false;
-
- if ( isNull(str) || isUndef(str) || (str+"" == "") )
- isValid = true;
-
- return isValid;
- } // end isBlank
-
- /* ======================================================================
- FUNCTION: isInt
-
- INPUT: numstr (string/number) - the string that will be tested to ensure
- that each character is a digit
- allowNegatives (boolean) - (optional) when true, allows numstr to be
- negative (contain a '-'). When false,
- any negative number or a string starting
- with a '-' will be considered invalid.
-
- RETURN: true, if all characters in the string are a character from 0-9,
- regardless of value for allowNegatives
- true, if allowNegatives is true and the string starts with a '-', and all other
- characters are 0-9.
- false, otherwise.
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isInt( numstr, allowNegatives ) {
- // Return immediately if an invalid value was passed in
- if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
- return false;
-
- // Default allowNegatives to true when undefined or null
- if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null")
- allowNegatives = true;
-
- var isValid = true;
-
- // convert to a string for performing string comparisons.
- numstr += "";
-
- // Loop through string and test each character. If any
- // character is not a number, return a false result.
- // Include special case for negative numbers (first char == '-').
- for (i = 0; i < numstr.length; i++) {
- if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
- isValid = false;
- break;
- } else if ((numstr.charAt(i) == "-" && i != 0) ||
- (numstr.charAt(i) == "-" && !allowNegatives)) {
- isValid = false;
- break;
- }
-
- } // END for
-
- return isValid;
- } // end isInt
-
- /* ======================================================================
- FUNCTION: isNull
-
- INPUT: val - the value to be tested
-
- RETURN: true, if the value is null;
- false, otherwise.
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isNull( val ) {
- var isValid = false;
-
- if (val+"" == "null")
- isValid = true;
-
- return isValid;
- } // end isNull
-
- /* ======================================================================
- FUNCTION: isNum
-
- INPUT: numstr (string/number) - the string that will be tested to ensure
- that the value is a number (int or float)
-
- RETURN: true, if all characters represent a valid integer or float
- false, otherwise.
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isNum( numstr ) {
- // Return immediately if an invalid value was passed in
- if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
- return false;
-
- var isValid = true;
- var decCount = 0; // number of decimal points in the string
-
- // convert to a string for performing string comparisons.
- numstr += "";
-
- // Loop through string and test each character. If any
- // character is not a number, return a false result.
- // Include special cases for negative numbers (first char == '-')
- // and a single decimal point (any one char in string == '.').
- for (i = 0; i < numstr.length; i++) {
- // track number of decimal points
- if (numstr.charAt(i) == ".")
- decCount++;
-
- if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") ||
- (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
- isValid = false;
- break;
- } else if ((numstr.charAt(i) == "-" && i != 0) ||
- (numstr.charAt(i) == "." && numstr.length == 1) ||
- (numstr.charAt(i) == "." && decCount > 1)) {
- isValid = false;
- break;
- }
- //if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) ||
- } // END for
-
- return isValid;
- } // end isNum
-
- /* ======================================================================
- FUNCTION: isUndef
-
- INPUT: val - the value to be tested
-
- RETURN: true, if the value is undefined
- false, otherwise.
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isUndef( val ) {
- var isValid = false;
-
- if (val+"" == "undefined")
- isValid = true;
-
- return isValid;
- } // end isUndef
-
- /* ======================================================================
- FUNCTION: isValid5DigitZip
-
- INPUT: str (string) - a 5-digit zip code to be tested
-
- RETURN: true, if the string is 5-digits long
- false, otherwise
-
- CALLS: isBlank(), isInt() which are defined elsewhere in the Script Library
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isValid5DigitZip( str ) {
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
- return false;
-
- var isValid = true;
-
- str += "";
-
- // Rules: zipstr must be 5 characters long, and can only contain numbers from
- // 0 through 9
- if (isBlank(str) || (str.length != 5) || !isInt(str, false))
- isValid = false;
-
- return isValid;
- } // end isValid5DigitZip
-
- /* ======================================================================
- FUNCTION: isValid5Plus4DigitZip
-
- INPUT: str (string) - a 5+4 digit zip code to be tested
-
- RETURN: true, if the string contains 5-digits followed by a dash followed by 4 digits
- false, otherwise
-
- CALLS: isBlank(), isInt() which are defined elsewhere in the Script Library
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isValid5Plus4DigitZip( str ) {
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
- return false;
-
- var isValid = true;
-
- str += "";
-
- // Rules: The first five characters may contain only digits 0-9,
- // the 6th character must be a dash, '-', and
- // the last four characters may contain only digits 0-9.
- // The total string length must be 10 characters.
- if (isBlank(str) || (str.length != 10) ||
- !isInt(str.substring(0,5), false) || str.charAt(5) != '-' ||
- !isInt(str.substring(6,10), false))
- isValid = false;
-
- return isValid;
- } // end isValid5Plus4DigitZip
-
- /* ======================================================================
- FUNCTION: isValidEmail
-
- INPUT: str (string) - an e-mail address to be tested
-
- RETURN: true, if the string contains a valid e-mail address which is a string
- plus an '@' character followed by another string containing at least
- one '.' and ending in an alpha (non-punctuation) character.
- false, otherwise
-
- CALLS: isBlank(), isAlpha() which are defined elsewhere in the Script Library
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isValidEmail( str ) {
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
- return false;
-
- var isValid = true;
-
- str += "";
-
- namestr = str.substring(0, str.indexOf("@")); // everything before the '@'
- domainstr = str.substring(str.indexOf("@")+1, str.length); // everything after the '@'
-
- // Rules: namestr cannot be empty, or that would indicate no characters before the '@',
- // domainstr must contain a period that is not the first character (i.e. right after
- // the '@'). The last character must be an alpha.
- if (isBlank(str) || (namestr.length == 0) ||
- (domainstr.indexOf(".") <= 0) ||
- (domainstr.indexOf("@") != -1) ||
- !isAlpha(str.charAt(str.length-1)))
- isValid = false;
-
- return isValid;
- } // end isValidEmail
-
- /* ======================================================================
- FUNCTION: isValidPhone
-
- INPUT: str (string) - an phone number to be tested
- incAreaCode (boolean) - if true, area code is included (10-digits);
- if false or undefined, area code not included
-
- RETURN: true, if the string contains a 7-digit phone number and incAreaCode == false
- or is undefined
- true, if the string contains a 10-digit phone number and incAreaCode == true
- false, otherwise
-
- CALLS: StripNonNumeric(), which is defined elsewhere in the Script Library
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isValidPhone( str, incAreaCode ) {
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
- return false;
-
- // Set default value for incAreaCode to false, if undefined or null
- if (incAreaCode+"" == "undefined" || incAreaCode+"" == "null")
- incAreaCode = false;
-
- var isValid = true;
-
- str += "";
-
- // After stripping out non-numeric characters, such as dashes, the
- // phone number should contain 7 digits (no area code) or 10 digits (area code)
- str = StripNonNumeric(str+"");
- if (incAreaCode && str.length != 10)
- isValid = false;
- if (!incAreaCode && str.length != 7)
- isValid = false;
-
- return isValid;
- } // end isValidPhone
-
- /* ======================================================================
- FUNCTION: isValidSSN
-
- INPUT: str (string) - an phone number to be tested
- incDashes (boolean) - if true, str includes dashes (e.g. 111-12-3456);
- if false, str contains only digits
-
- RETURN: true, if the string contains digits and dashes in the form 111-12-3456;
- true, if the string contains a 9-digit number and incDashes is false;
- false, otherwise
-
- CALLS: isInt(), which is defined elsewhere in the Script Library
-
- PLATFORMS: Netscape Navigator 3.01 and higher,
- Microsoft Internet Explorer 3.02 and higher,
- Netscape Enterprise Server 3.0,
- Microsoft IIS/ASP 3.0.
- ====================================================================== */
- function isValidSSN( str, incDashes ) {
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
- return false;
-
- var isValid = true;
-
- // Set default value for incDashes to true, if undefined or null
- if (incDashes+"" == "undefined" || incDashes+"" == "null")
- incDashes = true;
-
- str += ""; // make sure it's a string
-
- if (!incDashes && (!isNum(str) || str.length != 9))
- isValid = false;
-
- var part1 = str.substring(0,3);
- var part2 = str.substring(4,6);
- var part3 = str.substring(7,str.length);
-
- // Ensure that the first part is a number and 3 digits long,
- // the second part is a number and 2 digits long,
- // the third part is a number and 4 digits long, e.g. 111-22-3333
- if (incDashes && ((!isInt(part1, false) || part1.length != 3) ||
- (!isInt(part2, false) || part2.length != 2) ||
- (!isInt(part3, false) || part3.length != 4)) )
- isValid = false;
-
- return isValid;
- } // end isValidSSN
-
- /* ======================================================================
- FUNCTION: StripNonNumeric
-
- INPUT: str (string) - a string to be altered
-
- RETURN: a string containing only numeric characters 0-9;
- returns null if invalid arguments were passed
-
- DESC: This function removes all non-numeric characters from a given
- string. It is useful for removing dashes, parentheses, etc. from input
- strings such as credit card numbers or phone nubmers.
- ====================================================================== */
- function StripNonNumeric( str ) {
- var resultStr = "";
-
- // Return immediately if an invalid value was passed in
- if (str+"" == "undefined" || str == null)
- return null;
-
- // Make sure the argument is a string
- str += "";
-
- // Loop through entire string, adding each character from the original
- // string if it is a number
- for (var i=0; i < str.length; i++)
- {
- if ( (str.charAt(i) >= "0") && (str.charAt(i) <= "9") )
- resultStr = resultStr + str.charAt(i);
-
- } // end for loop
-
- return resultStr;
- } // end StripNonNumeric
-
- }
-
-