home *** CD-ROM | disk | FTP | other *** search
- /* ======================================================================
- OBJECT: com_netobjects_TextBox
-
- PLATFORMS: Netscape Navigator 4.01 or higher,
- Microsoft Internet Explorer 4.02 or higher
-
- DESC: This object allows developers to create TextBoxes with common built
- in behaviors such as formating and validation.
-
- Validation:
- ----------
- Validation will only occur when the validate property is true. See the
- headers of functions below with the same name as the desired
- valueType to find the validation rules for each valueType.
-
- Formatting:
- ----------
- none: The value will be displayed as entered.
- yes/no: values of 0, false or "no" will be formatted as "no"
- values of 1, -1, true or "yes" will be formatted as "yes"
- true/false: values of 0, false or "no" will be formatted as "false"
- values of 1, -1, true or "yes" will be formatted as "true"
- percent: numeric value will be multiplied by 100 and have a trailing "%"
- fixed: numeric value will be rounded to the number of decimal places specified
- in the object's decPlaces property.
- currency: numeric value will be rounded to the number of decimal places specified
- in the object's decPlaces property, and preceeded by a "$"
-
- ====================================================================== */
- function com_netobjects_TextBox(params) {
- // Public Properties
- this.name = (params.name != null) ? params.name : "";
- this.textBoxName = (params.textBoxName != null) ? params.textBoxName : "";
- this.formName = (params.formName != null) ? params.formName : "";
- this.value = (params.value != null) ? params.value : "";
- this.size = (params.size != null) ? params.size : "";
- this.isPassword = ((params.isPassword != null) && (params.isPassword == true)) ? params.isPassword : false;
- this.maxLength = (params.maxLength != null) ? params.maxLength : "";
- // none, 0/1, 0/-1, Yes/No, True/False, Percent, Currency, Fixed
- this.format = (params.format != null) ? params.format : "";
- this.decPlaces = ((params.decPlaces != null) && (params.decPlaces != "")) ? params.decPlaces : "0";
- this.validate = ((params.validate != null) && (params.validate == true)) ? params.validate : "";
- this.isRequired = (params.isRequired != null) ? params.isRequired : "";
- // none, IsAlpha, IsAlphaNum, IsAlphaNumOrUnderscore, IsInt, IsNum, IsValid5DigitZip, IsValid5Plus4DigitZip, IsValidEmail, IsValidPhone, IsValidSSN
- this.valueType = (params.valueType != null) ? params.valueType : "";
- this.state = (params.state != null) ? params.state : "";
-
- this.testMode = (params.testMode != null) ? params.testMode : "none"
-
- // Private Properties
- this.isValid = true;
- this.errType = null;
- this.textBoxIndex = 0
-
- // Public Methods
- this.validateValue = _TextBox_validateValue;
- this.render = _TextBox_render;
-
- // Private Methods
- this.validateProperties = _TextBox_validateProperties;
- this.getFormattedValue = _TextBox_GetFormattedValue;
- this.isValidFormatType = _TextBox_isValidFormatType
-
- // Public Events
- this.onBlur = _TextBox_onBlur;
- this.onChange = _TextBox_onChange;
- this.onFocus = _TextBox_onFocus;
- this.onSelect = _TextBox_onSelect;
- this.onValidate = _TextBox_onValidate;
- this.onInvalid = _TextBox_onInvalid;
- this.onValid = _TextBox_onValid;
- this.onRender = _TextBox_onRender;
-
- // Private Events (internal event driven functionality)
- this.eOnChange = _TextBox_eOnChange;
-
- // Validate object properties
- this.validateProperties();
-
- // Public Methods ----------------------------------------
- function _TextBox_render() {
- // Initialize Variables
- var sTextBoxName = "";
- var svalue = "";
- var smaxLength = "";
- var sisPassword = "";
- var ssize = "";
-
- var sonBlur = "";
- var sonChange = "";
- var sonFocus = "";
- var sonSelect = "";
-
- // Set Variables
- sTextBoxName = " Name = \"" + this.textBoxName + "\"";
- var fmtedVal = this.getFormattedValue();
- // Only format if it's not a password value
- if (!this.isPassword) {
- this.value = fmtedVal;
- svalue = " Value = \"" + fmtedVal + "\"";
- } else
- svalue= " Value = \"" + this.value + "\"";
- smaxLength = " MaxLength = \"" + this.maxLength + "\"";
- ssize = " Size = \"" + this.size + "\"";
-
- if (this.isPassword) {
- sisPassword = " Type = \"Password\""
- } else {
- sisPassword = " Type = \"Text\""
- }
-
- // Build Nav & IE shared link events
- sonBlur = " onBlur=\"" + this.name + ".onBlur();\""
- sonChange = " onchange=\"return " + this.name + ".eOnChange();\""
- sonFocus = " onFocus=\"" + this.name + ".onFocus();\""
- sonSelect = " onSelect=\"" + this.name + ".onSelect();\""
- eEvents = sonBlur + sonChange + sonFocus + sonSelect;
-
- // Write HTML
- if (this.state == "hidden") {
- document.write( "<Input Type=hidden" + sTextBoxName + svalue + eEvents + ">\n");
- } else if (this.state == "read-only") {
- document.write( fmtedVal + "\n");
- } else {
- document.write( "<Input" + sisPassword +sTextBoxName + svalue + ssize + smaxLength + eEvents + ">\n");
- }
-
- // Set control index:
- // The control index is used by the object to identify its respective form element
- // in the event that there is more than one form element with the same name.
- if (this.state != "read-only") {
- // Set control index if multiple controls exist
- this.textBoxIndex = eval("window.document." + this.formName + "." + this.textBoxName + ".length")-1
- if (isNaN(this.textBoxIndex)) {this.textBoxIndex=0}
- }
-
- // Fire public events
- this.onRender();
-
- } // END _TextBox_render
-
-
- /* ======================================================================
-
- METHOD: _TextBox_validateProperties. If the test mode is set,
- generate error and/or warning messages if required or recommended properties
- are set with invalid property values or are not set at all.
-
- ====================================================================== */
- function _TextBox_validateProperties() {
- if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
- var errors = new Array()
- iIndex = 0;
-
- // Check for errors
- if (this.name == "")
- errors[iIndex++] = "Name is a required property.";
- if (this.textBoxName == "")
- errors[iIndex++] = "Textbox name is a required property.";
- if (this.textBoxName == this.name)
- errors[iIndex++] = "Textbox name and object name may not be the same.";
- if (!this.isValidFormatType(this.format, this.valueType))
- if (this.format == "Fixed" && this.valueType == "IsInt")
- errors[iIndex++] = "The format " + this.format + " is not valid with value type " + this.valueType + " unless decPlaces is set to 0.";
- else
- errors[iIndex++] = "The format " + this.format + " is not valid with value type " + this.valueType + ".";
- if ((this.isPassword) && !((this.valueType == "") || (this.valueType == "none")))
- errors[iIndex++] = "You cannot have a value type on password values.";
- if ((this.isPassword) && !((this.format == "") || (this.format == "none")))
- errors[iIndex++] = "You cannot format a password value.";
-
- // Display errors
- for (var i=0; i<errors.length; i++) {
- if (this.testMode.toLowerCase() == "text")
- document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_Button, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n");
- else
- alert("TEST MODE, com_netobjects_TextBox, '" + this.name + "' : " + errors[i]);
-
- }
- }
- } // END _TextBox_validateProperties
-
- /* ======================================================================
-
- METHOD: _TextBox_validateValue. Validates the value entered in the text
- box according to the valueType property. Validation will only occur when
- the validate property is true. See the headers of functions below with
- the same name as the desired valueType to find the validation rules for
- each valueType. This function will also make sure the value has not
- been left blank if the isRequired property is true.
-
- ====================================================================== */
- function _TextBox_validateValue() {
- this.isValid = true;
- this.errType = ""
-
- // Check for required value
- if ((this.isRequired) && ((this.value == null) || (this.value == ""))) {
- this.errType = "NoValue"
- this.isValid = false;
- // exit function
- }
-
- // Check for value type
- if (this.isValid) {
- if ((this.valueType == "IsAlpha") && (!IsAlpha(this.value))) {
- this.errType = "NotAlpha"
- this.isValid = false;
- } else if ((this.valueType == "IsAlphaNum") && (!IsAlphaNum(this.value))) {
- this.errType = "NotAlphaNum"
- this.isValid = false;
- } else if ((this.valueType == "IsAlphaNumOrUnderscore") && (!IsAlphaNumOrUnderscore(this.value))) {
- this.errType = "NotAlphaNumOrUnderscore"
- this.isValid = false;
- } else if ((this.valueType == "IsInt") && (!IsInt(this.value))) {
- this.errType = "NotInt"
- this.isValid = false;
- } else if ((this.valueType == "IsNum") && (!IsNum(this.value))) {
- this.errType = "NotNum"
- this.isValid = false;
- } else if ((this.valueType == "IsValid5DigitZip") && (!IsValid5DigitZip(this.value))) {
- this.errType = "NotValid5DigitZip"
- this.isValid = false;
- } else if ((this.valueType == "IsValid5Plus4DigitZip") && (!IsValid5Plus4DigitZip(this.value))) {
- this.errType = "NotValid5Plus4DigitZip"
- this.isValid = false;
- } else if ((this.valueType == "IsValidEmail") && (!IsValidEmail(this.value))) {
- this.errType = "NotValidEmail"
- this.isValid = false;
- } else if ((this.valueType == "IsValidPhone") && (!IsValidPhone(this.value))) {
- this.errType = "NotValidPhone"
- this.isValid = false;
- } else if ((this.valueType == "IsValidSSN") && (!IsValidSSN(this.value))) {
- this.errType = "NotValidSSN"
- this.isValid = false;
- }
-
- }
-
-
- // Fire public events
- if (this.isValid) {this.onValid()} else {this.onInvalid()};
- this.onValidate()
-
- return this.isValid;
-
- } // END _TextBox_validate
-
-
-
- // Private Event Handlers ----------------------------------------
- /* ======================================================================
-
- METHOD: _TextBox_isValidFormatType. Determines whether combination of
- format and valueType are valid together.
-
- ====================================================================== */
- function _TextBox_isValidFormatType(valFormat, valType) {
- var result = false
- if ((valFormat == null) || (valType == null)||
- (valFormat == "none") || (valType == "none") ||
- (valFormat == "") || (valType == "")) {
- result = true
- } else if (((valFormat == "Yes/No") || (valFormat == "True/False"))
- && ((valType == "IsAlpha") || (valType == "IsAlphaNum")
- ||(valType == "IsAlphaNumOrUnderscore"))) {
- result = true
- } else if ((valFormat == "Fixed")
- && ((valType == "IsInt" && this.decPlaces == 0) || (valType == "IsNum"))) {
- result = true
- }
-
- return result
-
- } // END _TextBox_isValidFormatType
-
- /* ======================================================================
-
- METHOD: _TextBox_eOnChange. Internal on change event. Updates the value
- from the textbox.
-
- ====================================================================== */
- function _TextBox_eOnChange() {
- // use control index if more than one control exists
- var index = ""
- if (!isNaN(eval("window.document." + this.formName + "." + this.textBoxName + ".length"))) {
- index = "[" + this.textBoxIndex + "]"
- }
-
- // update value
- this.value = eval("window.document." + this.formName + "." + this.textBoxName + index + ".value")
- var fmtedVal = this.getFormattedValue();
- if ((this.value != fmtedVal) && !this.isPassword) {
- this.value = fmtedVal
- eval("window.document." + this.formName + "." + this.textBoxName + index + ".value=\"" + fmtedVal+"\"")
- }
-
- var result = true
- if (this.validate) {
- result = this.validateValue()
- }
-
- // Fire public events
- this.onChange()
-
- return result
-
- } // END _TextBox_eOnChange
-
- /* ======================================================================
-
- METHOD: _TextBox_GetFormattedValue. Formats textbox value according to
- the value of the format property.
-
- none: The value will be displayed as entered.
- yes/no: values of 0, false or "no" will be formatted as "no"
- values of 1, -1, true or "yes" will be formatted as "yes"
- true/false: values of 0, false or "no" will be formatted as "false"
- values of 1, -1, true or "yes" will be formatted as "true"
- percent: numeric value will be multiplied by 100 and have a trailing "%"
- fixed: numeric value will be rounded to the number of decimal places specified
- in the object's decPlaces property.
- currency: numeric value will be rounded to the number of decimal places specified
- in the object's decPlaces property, and preceeded by a "$"
-
- ====================================================================== */
- function _TextBox_GetFormattedValue() {
- if ((this.format == "Yes/No") || (this.format == "True/False")) {
- return FormatBoolean(this.value, this.format)
- } else if ((this.format != "")) { // Format number
- return FormatNumber(this.value, this.format, this.decPlaces)
- } else {
- return this.value
- }
-
- } // END _TextBox_GetFormattedValue
-
- // Public Event Handlers ----------------------------------------
- function _TextBox_onBlur() {
- }
- function _TextBox_onChange() {
- }
- function _TextBox_onFocus() {
- }
- function _TextBox_onSelect() {
- }
- function _TextBox_onValidate() {
- }
- function _TextBox_onInvalid() {
- // Error messages are put here so that they may be overridden
- if (this.errType == "NoValue") {
- alert("An entry in this field is REQUIRED.")
- } else if (this.errType == "NotAlpha") {
- alert("An entry in this field may only contain alpha characters.")
- } else if (this.errType == "NotAlphaNum") {
- alert("An entry in this field may only contain alpha-numeric characters.")
- } else if (this.errType == "NotAlphaNumOrUnderscore") {
- alert("An entry in this field may only contain alpha-numeric characters and underscores.")
- } else if (this.errType == "NotInt") {
- alert("An entry in this field must be an integer.")
- } else if (this.errType == "NotNum") {
- alert("An entry in this field must be an number.")
- } else if (this.errType == "NotValid5DigitZip") {
- alert("An entry in this field must be a valid 5 digit zipcode.")
- } else if (this.errType == "NotValid5Plus4DigitZip") {
- alert("An entry in this field must be a valid 5 plus 4 digit zipcode.")
- } else if (this.errType == "NotValidEmail") {
- alert("An entry in this field must be a valid email.")
- } else if (this.errType == "NotValidPhone") {
- alert("An entry in this field must be a valid phone.")
- } else if (this.errType == "NotValidSSN") {
- alert("An entry in this field must be a valid social security number.")
- } else {
- alert("Unknown Error.")
- }
- }
- function _TextBox_onValid() {
- }
- function _TextBox_onRender() {
- }
-
-
-
- } // END CONSTRUCTOR com_netobjects_TextBox
-
- // Shared Functions ---------------------------------------------------------------
- /* ======================================================================
-
- FUNCTION: FormatBoolean. Formats boolean values "Yes/No", "True/False", "-1/0" & "1/0"
- Fails silently, returning original value
-
- ====================================================================== */
- function FormatBoolean(myVal, FmtName) {
- var newVal = myVal
- var ValIsTrue = false
- var ValIsFalse = false
-
- if ((myVal.toString() == 0) ||
- (myVal.toString().toLowerCase() == "false") ||
- (myVal.toString().toLowerCase() == "no")){
- ValIsFalse = true
- } else if ((myVal.toString() == "-1") || (myVal.toString() == "1") ||
- (myVal.toString().toLowerCase() == "true") ||
- (myVal.toString().toLowerCase() == "yes")) {
- ValIsTrue = true
- }
-
- if (FmtName == "Yes/No") {
- if (ValIsTrue){ newVal = "Yes" } else if (ValIsFalse){ newVal = "No" }
- } else if (FmtName == "True/False") {
- if (ValIsTrue){ newVal = "True" } else if (ValIsFalse){ newVal = "False" }
- } else if (FmtName == "-1/0") {
- if (ValIsTrue){ newVal = -1 } else if (ValIsFalse){ newVal = 0 }
- } else if (FmtName == "1/0") {
- if (ValIsTrue){ newVal = 1 } else if (ValIsFalse){ newVal = 0 }
- }
-
- return newVal
- } // END FormatBoolean
-
-
- /* ======================================================================
-
- FUNCTION: FormatNumber. Formats numeric values "Percent", "Currency" & "Fixed"
- Fails silently, returning original value
-
- ====================================================================== */
- function FormatNumber(myVal, FmtName, decPlaces) {
- var newVal = myVal
-
- // Strip out spaces...
-
- // if Percent strip off '%'
- if ((FmtName == "Percent") && (myVal.substring(myVal.length-1,myVal.length) == "%")) {
- myVal = myVal.substring(0,myVal.length-1)
- if (IsNum(myVal)) {myVal = myVal/100}
- }
- // if Currency strip off '$'
- if ((FmtName == "Currency") && (myVal.substring(0,1) == "$")) {
- myVal = myVal.substring(1,myVal.length)
- }
-
- if (IsNum(myVal)) {
-
- // Percent, Currency, Fixed0Places, Fixed2Places, Fixed3Places, Fixed4Places, Fixed5Places
- if (FmtName == "Percent") {
- // var newDec = (parseInt(decPlaces,10)+2)
- newVal = (Fix(myVal*100, decPlaces))+"%"
- } else if (FmtName == "Currency") {
- newVal = "$"+Fix(myVal, decPlaces)
- } else if (FmtName == "Fixed") {
- newVal = Fix(myVal, decPlaces)
- }
-
- }
-
- return newVal
- } // END FormatNumber
-
- /* ======================================================================
-
- FUNCTION: Fix. Rounds to specified decimal point.
- Assumes a number is passed.
-
- ====================================================================== */
- function Fix(myVal, decPlaces) {
- var newVal = myVal
- var returnVal = 0;
-
- var test = (Math.round(myVal*Math.pow(10,decPlaces))).toString() //Math.round(myVal*Math.pow(10,decPlaces))/Math.pow(10,decPlaces)
- if (decPlaces > 0)
- returnVal = test.substring(0,test.length-decPlaces) + "." + test.substring(test.length-decPlaces,test.length);
- else if (decPlaces == 0)
- returnVal = test.substring(0,test.length-decPlaces);
- else
- returnVal = myVal;
-
- return returnVal;
- } // END Fix
-
- /* ======================================================================
- 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: 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: 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: 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: 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: 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: 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: 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
-
-
- // Shared Array Parsing function
- function unEncodeJS(input) {
- if (null == input)
- return null;
- if (input == "null" )
- return null;
- output = "";
- i=0;
- while (i<input.length) {
- ch = input.charAt(i++);
- if ('\\' == ch) {
- ch = input.charAt(i++);
- if ('n' == ch) {
- output += '\n';
- } else if (';' == ch) {
- output += ';';
- } else if ('r' == ch) {
- //supress this character
- } else if ('t' == ch) {
- output += '\t';
- } else {
- output += ch;
- }
- } else {
- output += ch;
- }
- }
- return output;
- } // END unEncodeList
-
- function unEncodeList( sList ) {
- aRes = null;
- if ( null != sList && ""!=sList ) {
- aRes = new Array();
- iStart = 0;
- iIndex = 0;
- j=0;
- while (j<sList.length) {
- ch = sList.charAt(j++);
- if ('\\' == ch) {
- ch = sList.charAt(j++);
- } else {
- if ( ';' == ch ) {
- sRaw = sList.substring( iStart, j-1 );
- aRes[iIndex++] = unEncodeJS( sRaw );
- iStart = j;
- }
- }
- }
- if ( iStart != j ) {
- sRaw = sList.substring( iStart, j );
- aRes[iIndex++] = unEncodeJS( sRaw );
- }
- }
- return aRes;
- } // END unEncodeList
-