📄 form_check.js.svn-base
字号:
// isUSPhoneNumber (STRING s [, BOOLEAN emptyOK])
//
// isUSPhoneNumber returns true if string s is a valid U.S. Phone
// Number. Must be 10 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, parentheses, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isUSPhoneNumber (s)
{ if (isEmpty(s))
if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
else return (isUSPhoneNumber.arguments[1] == true);
return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}
// isInternationalPhoneNumber (STRING s [, BOOLEAN emptyOK])
//
// isInternationalPhoneNumber returns true if string s is a valid
// international phone number. Must be digits only; any length OK.
// May be prefixed by + character.
//
// NOTE: A phone number of all zeros would not be accepted.
// I don't think that is a valid phone number anyway.
//
// NOTE: Strip out any delimiters (spaces, hyphens, parentheses, etc.)
// from string s before calling this function. You may leave in
// leading + character if you wish.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isInternationalPhoneNumber (s)
{ if (isEmpty(s))
if (isInternationalPhoneNumber.arguments.length == 1) return defaultEmptyOK;
else return (isInternationalPhoneNumber.arguments[1] == true);
return (isPositiveInteger(s))
}
// isZIPCode (STRING s [, BOOLEAN emptyOK])
//
// isZIPCode returns true if string s is a valid
// U.S. ZIP code. Must be 5 or 9 digits only.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isZIPCode (s)
{ if (isEmpty(s))
if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
else return (isZIPCode.arguments[1] == true);
return (isInteger(s) &&
((s.length == digitsInZIPCode1) ||
(s.length == digitsInZIPCode2)))
}
// isStateCode (STRING s [, BOOLEAN emptyOK])
//
// Return true if s is a valid U.S. Postal Code
// (abbreviation for state).
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isStateCode(s)
{ if (isEmpty(s))
if (isStateCode.arguments.length == 1) return defaultEmptyOK;
else return (isStateCode.arguments[1] == true);
return ( (USStateCodes.indexOf(s) != -1) &&
(s.indexOf(USStateCodeDelimiter) == -1) )
}
// isEmail (STRING s [, BOOLEAN emptyOK])
//
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isEmail (s)
{ if (isEmpty(s))
if (isEmail.arguments.length == 1) return defaultEmptyOK;
else return (isEmail.arguments[1] == true);
// is s whitespace?
if (isWhitespace(s)) return false;
// there must be >= 1 character before @, so we
// 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;
}
// isYear (STRING s [, BOOLEAN emptyOK])
//
// isYear returns true if string s is a valid
// Year number. Must be 2 or 4 digits only.
//
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isYear (s)
{ if (isEmpty(s))
if (isYear.arguments.length == 1) return defaultEmptyOK;
else return (isYear.arguments[1] == true);
if (!isNonnegativeInteger(s)) return false;
return ((s.length == 2) || (s.length == 4));
}
// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
//
// isIntegerInRange returns true if string s is an integer
// within the range of integer arguments a and b, inclusive.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isIntegerInRange (s, a, b)
{ if (isEmpty(s))
if (isIntegerInRange.arguments.length == 3) return defaultEmptyOK;
else return (isIntegerInRange.arguments[3] == true);
// Catch non-integer strings to avoid creating a NaN below,
// which isn't available on JavaScript 1.0 for Windows.
if (!isInteger(s, false)) return false;
// Now, explicitly change the type to integer via parseInt
// so that the comparison code below will work both on
// JavaScript 1.2 (which typechecks in equality comparisons)
// and JavaScript 1.1 and before (which doesn't).
var num = parseInt (s);
return ((num >= a) && (num <= b));
}
// isMonth (STRING s [, BOOLEAN emptyOK])
//
// isMonth returns true if string s is a valid
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isMonth (s)
{ if (isEmpty(s))
if (isMonth.arguments.length == 1) return defaultEmptyOK;
else return (isMonth.arguments[1] == true);
return isIntegerInRange (s, 1, 12);
}
// isDay (STRING s [, BOOLEAN emptyOK])
//
// isDay returns true if string s is a valid
// day number between 1 and 31.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isDay (s)
{ if (isEmpty(s))
if (isDay.arguments.length == 1) return defaultEmptyOK;
else return (isDay.arguments[1] == true);
return isIntegerInRange (s, 1, 31);
}
// daysInFebruary (INTEGER year)
//
// Given integer argument year,
// returns number of days in February of that year.
function daysInFebruary (year)
{ // February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}
// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day
// form a valid date.
//
function isDate (year, month, day)
{ // catch invalid years (not 2- or 4-digit) and invalid months and days.
if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
// Explicitly change type to integer to make code work in both
// JavaScript 1.1 and JavaScript 1.2.
var intYear = parseInt(year);
var intMonth = parseInt(month);
var intDay = parseInt(day);
// catch invalid days, except for February
if (intDay > daysInMonth[intMonth]) return false;
if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
return true;
}
/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */
// Display prompt string s in status bar.
function prompt (s)
{ window.status = s
}
// Display data entry prompt string s in status bar.
function promptEntry (s)
{ window.status = pEntryPrompt + s
}
// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.
function warnEmpty (theField, s)
{ theField.focus()
alert(mPrefix + s + mSuffix)
return false
}
// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.
function warnInvalid (theField, s)
{ theField.focus()
theField.select()
alert(s)
return false
}
/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */
// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkString (theField, s, emptyOK)
{ // Next line is needed on NN3 to avoid "undefined is not a number" error
// in equality comparison below.
if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
if ((emptyOK == true) && (isEmpty(theField.value))) return true;
if (isWhitespace(theField.value))
return warnEmpty (theField, s);
else return true;
}
// checkStateCode (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid U.S. state code.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkStateCode (theField, emptyOK)
{ if (checkStateCode.arguments.length == 1) emptyOK = defaultEmptyOK;
if ((emptyOK == true) && (isEmpty(theField.value))) return true;
else
{ theField.value = theField.value.toUpperCase();
if (!isStateCode(theField.value, false))
return warnInvalid (theField, iStateCode);
else return true;
}
}
// takes ZIPString, a string of 5 or 9 digits;
// if 9 digits, inserts separator hyphen
function reformatZIPCode (ZIPString)
{ if (ZIPString.length == 5) return ZIPString;
else return (reformat (ZIPString, "", 5, "-", 4));
}
// checkZIPCode (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid ZIP code.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkZIPCode (theField, emptyOK)
{ if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
if ((emptyOK == true) && (isEmpty(theField.value))) return true;
else
{ var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
if (!isZIPCode(normalizedZIP, false))
return warnInvalid (theField, iZIPCode);
else
{ // if you don't want to insert a hyphen, comment next line out
theField.value = reformatZIPCode(normalizedZIP)
return true;
}
}
}
// takes USPhone, a string of 10 digits
// and reformats as (123) 456-789
function reformatUSPhone (USPhone)
{ return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}
// checkUSPhone (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid US Phone.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkUSPhone (theField, emptyOK)
{ if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
if ((emptyOK == true) && (isEmpty(theField.value))) return true;
else
{ var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
if (!isUSPhoneNumber(normalizedPhone, false))
return warnInvalid (theField, iUSPhone);
else
{ // if you don't want to reformat as (123) 456-789, comment next line out
theField.value = reformatUSPhone(normalizedPhone)
return true;
}
}
}
// checkInternationalPhone (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid International Phone.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkInternationalPhone (theField, emptyOK)
{ if (checkInternationalPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
if ((emptyOK == true) && (isEmpty(theField.value))) return true;
else
{ if (!isInternationalPhoneNumber(theField.value, false))
return warnInvalid (theField, iWorldPhone);
else return true;
}
}
// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkEmail (theField, emptyOK)
{ if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
if ((emptyOK == true) && (isEmpty(theField.value))) return true;
else if (!isEmail(theField.value, false))
return warnInvalid (theField, iEmail);
else return true;
}
// takes SSN, a string of 9 digits
// and reformats as 123-45-6789
function reformatSSN (SSN)
{ return (reformat (SSN, "", 3, "-", 2, "-", 4))
}
// Check that string theField.value is a valid SSN.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkSSN (theField, emptyOK)
{ if (checkSSN.arguments.length == 1) emptyOK = defaultEmptyOK;
if ((emptyOK == true) && (isEmpty(theField.value))) return true;
else
{ var normalizedSSN = stripCharsInBag(theField.value, SSNDelimiters)
if (!isSSN(normalizedSSN, false))
return warnInvalid (theField, iSSN);
else
{ // if you don't want to reformats as 123-456-7890, comment next line out
theField.value = reformatSSN(normalizedSSN)
return true;
}
}
}
// Check that string theField.value is a valid Year.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkYear (theField, emptyOK)
{ if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
if ((emptyOK == true) && (isEmpty(theField.value))) return true;
if (!isYear(theField.value, false))
return warnInvalid (theField, iYear);
else return true;
}
// Check that string theField.value is a valid Month.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -