/* *************************************************************
** FORMVAL.JS - JS Form Validation Library
** =======================================
** This library contains code to validate HTML form-field data.
** The bulk of this file was derived from Eric Krock's FormChek.js at
** developer.netscape.com/docs/examples/javascript/formval/overview.html
** (Many thanks, Eric!) Enjoy ... and please maintain this header.
**
** To load this library in an HTML doc, put the following
** line in the doc's HEAD (before any other SCRIPT tags):
**
**
**
** Author Ver Date Comments
** ====== === ==== ========
** Eric Krock 1.0 2/18/97 original JS 1.0-only version
** Rick Scott 2.0 2/1/00 streamlined file by removing:
** SS check, credit-card checks,
** defaultEmptyOK, and much more;
** added stripLeadingBlanks(),
** stripTrailingBlanks(),
** stripLeadingTrailingBlanks(),
** getCheckedRadioButton(),
** getCheckedCheckboxes(),
** getCheckedSelectOptions()
** S Scholtz 2.1 10/20/04 added date validation function, isDate()
** S Scholtz 2.2 06/17/05 added Canadian Postal Code validation function, isPostalCode()
** AhmedT 2.3 10/29/06 added isNotAlphaNumeric (to indicate failure when the field is not alpha by returning false of unifrom dynamic validation based on true/false only)
** AhmedT 2.3 11/30/06 changed isAlphaNumeric to include spaces as valid.
** AhmedT 2.3 12/18/06 changed phone number validation to use a regular expression. We now support 1800, and extensions
**
** (c) 1997 Netscape Communications Corporation
** Copyright 2000, Rick Scott, all rights reserved.
************************************************************* */
/* *************************************************************
** USAGE
** =====
** Functions to Validata Form-Element Data:
** isEmpty(s) - true if string s is empty
** isBlank(s) - true if string s is empty or blank (all whitespace chars)
** isLetter(c) - true if char c is an English letter
** isDigit(c) - true if char c is a digit
** isInteger(s) - true if string s is a signed/unsigned number
** isIntegerInRange(s, a, b) - true if string s (integer) is a <= s <= b
** isFloat(s) - true if string s is a signed/unsigned floating-point number
** isNotFloat(s) - return false if s is not float
** isAlphanumeric(s) - true if s is English letters and numbers only
** isNotAlphanumeric(s) - false if s is Not English letters and numbers only
** isUSPhoneNumber(s) - true if string s is a valid U.S. phone number
** isZIPCode(s) - true if string s is a valid U.S. ZIP code
** isPostalCode(s) - true if string s is a valid CDN Postal Code (A1B2C3)
** isStateCode(s) - true if string s is a valid U.S. (2-letter) state code
** isEmail(s) - true if string s is a valid email address
** getCheckedRadioButton(radioSet) - gets index checked radio button in set
** getCheckedCheckboxes(checkboxSet) - gets index(es) of checked checkboxes in set
** getCheckedSelectOptions(select) - gets index(es) of checked select options
** isDate(dtStr,dtCh,minYear,maxYear,dateFormat) - validates a date; date format can be "mdy","ymd" and "dmy"; "dtCh" is the date seperator (like "-" or "/")
**
** Functions to Reformat input=text Form-Field Data:
** stripCharsInBag(s, bag) - removes all chars in string bag from string s
** stripCharsNotInBag(s, bag) - removes all chars NOT in string bag from string s
** stripBlanks(s) - removes all blank chars from s
** stripLeadingBlanks(s) - removes leading blank chars from s
** stripTrailingBlanks(s) - removes trailing blank chars from s
** stripLeadingTrailingBlanks(s) - removes lead+trail blank chars from s
** reformatString(targetStr, str1, int1 [, str2, int2 [, ..., ...]]) -
** inserts formatting chars or delimiters in targetStr (see below)
************************************************************* */
/* ********************************************************** */
/* Global Variables and Constants *************************** */
/* ********************************************************** */
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var blanks = " \t\n\r"; // aka whitespace chars
// decimal point character differs by language and culture
var decimalPointDelimiter = ".";
// non-digit characters allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters allowed in US phone numbers
var validUSPhoneChars = digits + phoneNumberDelimiters;
// U.S. phone numbers have 10 digits, formatted as ### ### #### or (###)###-####
var digitsInUSPhoneNumber = 10;
// non-digit characters which are allowed in ZIP Codes
var ZIPCodeDelimiters = "-";
// our preferred delimiter for reformatting ZIP Codes
var ZIPCodeDelimeter = "-";
// U.S. ZIP codes have 5 or 9 digits, formatted as ##### or #####-####
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
// Valid U.S. Postal Codes for states, territories, armed forces, etc.
// See http://www.usps.gov/ncsc/lookups/abbr_state.txt
var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP|al|ak|as|az|ar|ca|co|ct|de|dc|fm|fl|ga|gu|hi|id|il|in|ia|ks|ky|la|me|mh|md|ma|mi|mn|ms|mo|mt|ne|nv|nh|nj|nm|ny|nc|nd|mp|oh|ok|or|pw|pa|pr|ri|sc|sd|tn|tx|ut|vt|vi|va|wa|wv|wi|wy|ae|aa|ae|ae|ap";
/* ********************************************************** */
/* Functions ************************************************ */
/* ********************************************************** */
// Returns true if string s is empty
function isEmpty(s)
{
return ((s == null) || (s.length == 0));
}
// Returns true if string s is empty or all blank chars
function isBlank(s)
{
var i;
// Is s empty?
if (isEmpty(s))
return true;
// Search through string's chars one by one until we find first
// non-blank char, then return false; if we don't, return true
for (i=0; i1) strDay=strDay.substring(1);
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
}
month=parseInt(strMonth);
day=parseInt(strDay);
year=parseInt(strYr);
if (pos1==-1 || pos2==-1){
return false;
}
if (strMonth.length<1 || month<1 || month>12){
return false;
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
return false;
}
if (strYear.length != 4 || year==0 || yearmaxYear){
return false;
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
return false;
}
return true;
}
function isNotDate (str) {
return !isDate (str, '-', 1900, 3000, 'ymd');
}
function isNotDateIfNotBlank (str) {
if (str != '')
return !isDate (str, '-', 1900, 3000, 'ymd');
else
return false;
}
function hacked_isNotDateDependingOnAnotherFieldThatCouldBeNumberOrBlank(dateField, otherField) {
if (otherField == '' && dateField == '')
return false;
else
return isNotDate(dateField);
}
// Removes all characters which appear in string bag from string s
function stripCharsInBag (s, bag)
{
var i;
var returnString = "";
// Search through string's characters one by one;
// if character is not in bag, append to returnString
for (i = 0; i < s.length; i++)
{
// Check that current character isn't blank
var c = s.charAt(i);
if (bag.indexOf(c) == -1)
returnString += c;
}
return returnString;
}
// Removes all characters which do NOT appear in string bag from string s
function stripCharsNotInBag (s, bag)
{
var i;
var returnString = "";
// Search through string's characters one by one;
// if character is in bag, append to returnString
for (i = 0; i < s.length; i++)
{
// Check that current character isn't blank
var c = s.charAt(i);
if (bag.indexOf(c) != -1)
returnString += c;
}
return returnString;
}
// Removes all blank chars (as defined by blanks) from s
function stripBlanks(s)
{
return stripCharsInBag(s, blanks)
}
// Removes leading blank chars (as defined by blanks) from s
function stripLeadingBlanks(s)
{
var i = 0;
while ((i < s.length) && (blanks.indexOf(s.charAt(i)) != -1))
i++;
return s.substring(i, s.length);
}
// Removes trailing blank chars (as defined by blanks) from s
function stripTrailingBlanks(s)
{
var i = s.length - 1;
while ((i >= 0) && (blanks.indexOf(s.charAt(i)) != -1))
i--;
return s.substring(0, i+1);
}
// Removes leading+trailing blank chars (as defined by blanks) from s
function stripLeadingTrailingBlanks(s)
{
s = stripLeadingBlanks(s);
s = stripTrailingBlanks(s);
return s;
}
// Returns true if character c is an English letter (A .. Z, a..z)
function isLetter(c)
{
return (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")));
}
// Returns true if character c is a digit (0 .. 9)
function isDigit(c)
{
return ((c >= "0") && (c <= "9"));
}
// Returns true if all chars in string s are numbers;
// first character is allowed to be + or -; does not
// accept floating point, exponential notation, etc.
function isInteger(s)
{
if (isBlank(s))
return false;
// skip leading + or -
if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
var i = 1;
else
var i = 0;
// Search through string's chars one by one until we find a
// non-numeric char, then return false; if we don't, return true
for (i; i= 1 character before @, so we start
// start looking at character position 1 (i.e. second character)
var i = 1;
var sLength = s.length;
// look for @
while ((i < sLength) && (s.charAt(i) != "@"))
i++
if ((i >= sLength) || (s.charAt(i) != "@"))
return false;
else
i += 2;
// look for .
while ((i < sLength) && (s.charAt(i) != "."))
i++
// there must be at least one character after the .
if ((i >= sLength - 1) || (s.charAt(i) != "."))
return false;
else
return true;
}
// Returns true if string s is an integer such that a <= s <= b
function isIntegerInRange (s, a, b)
{
if (isBlank(s))
return false;
if (!isInteger(s))
return false;
var num = parseInt(s);
return ((num >= a) && (num <= b));
}
// Returns index of checked radio button in radio set,
// or -1 if no radio buttons are checked
function getCheckedRadioButton(radioSet)
{
for (var i=0; i 0)
return arr;
else
return -1;
}
// Returns array containing index(es) of checked option(s)
// in select box, or -1 if no options are selected
function getCheckedSelectOptions(select)
{
var arr = new Array();
for (var i=0,j=0; i 0)
return arr;
else
return -1;
}
function isNotNumber(s)
{
return isNotFloat(s) && isNotInteger(s);
}
function isNoRadioSelected(radioSet)
{
return getCheckedRadioButton(radioSet) == -1;
}
function isNoCheckboxesSelected(checkboxSet)
{
return getCheckedCheckboxes(checkboxSet) == -1;
}
function isNotNumberIfNotBlank(s)
{
if (s == '') return false;
else return isNotFloat(s) && isNotInteger(s);
}
function isNotNumberOrIsBlank(s)
{
if (s == '') return true;
else return isNotFloat(s) && isNotInteger(s);
}
function isLongerThan255(s){
return (s.length > 255);
}
// This method augments the validation routines cause we have a special case here
// The value of the price field has to be either a valid number or 'n/a'. We accept n/a
// as an indication of the absence of a history record.
function isNotVaildPriceInYearHistory (s) {
if (s == 'n/a')
false;
else
return isNotNumber(s); // now that we checked our special n/a case, fallback to regular validation
}
// Yet another method that is specific
function isFieldAndDependantComboNotValid (fieldId, dependantFieldId, dependantPassValue) {
if (document.getElementById(dependantFieldId).value == dependantPassValue)
return false;
else
return true;
}