validation.js

来自「Java的框架」· JavaScript 代码 · 共 362 行

JS
362
字号
/*
 * This is a set of function that can be use for validating input of a HTML form.
 *
 * isEmpty								check if string is empty.
 * isWhitespace						Return true if string is empty or only contain whitspace character only.
 * isLetter								check if a english letter (A .. z, a .. z)
 * isDigit								check if is a digit (0 .. 9)
 * isLetterOrDigit				Returns true if character is a letter or digit.
 * isInteger							Check if is non-signed integers
 * isSignedInteger				check if is signed integers
 * isPositiveInteger			Check if is integer > 0
 * isNonnegativeInteger		Check if is integer >= 0
 * isNegativeInteger			Check if is integer < 0
 * isNonpositiveInteger		Check if is integer <= 0
 * isFloat								Check if is float number
 * isSignedFloat					Check if is signed float number
 * isAlphabetic						Check if is english letters.
 * isAlphanumeric					Check if is alphanumeric
 * isEmail								Check if is email
 * isIntegerInRange				Check if is integer within the specified range.
 * isMonth								Check if is valid month (1 .. 12) 
 * isYear									Check if is 2 or 4 digit year
 * isDay									Check if is valid day (1 .. 31)
 * daysInFebruary					Return the number of days in February for the specified year (Integer).
 * isDate									Check if the specified day, month, year is valid.
 * checkString						Check if string is of specified length
 *
 */

 
/* *******************************************
	Check if the string is empty.
******************************************** */	

function isEmpty (s) {   
	return ((s == null) || (s.length == 0))
}


/* ********************************************
	Return true if string is empty or only contain 
	whitspace character only.
********************************************* */

var reWhitespace = /^\s+$/

function isWhitespace (s) {
	return (isEmpty (s) || reWhitespace.test (s));
}


/* *********************************************
	Returns true if character c is an English letter
	(A .. Z, a..z).
********************************************** */

var reLetter = /^[a-zA-Z]$/
  
function isLetter (c) {   
	return reLetter.test(c)
}


/* **********************************************
 	Returns true if character c is a digit 
 	(0 .. 9).
*********************************************** */

var reDigit = /^\d/

function isDigit (c) {   
	return reDigit.test(c)
}


/* **********************************************
	Returns true if character c is a letter or digit.
*********************************************** */

var reLetterOrDigit = /^([a-zA-Z]|\d)$/

function isLetterOrDigit (c) {   
	return reLetterOrDigit.test(c)
}


/* **********************************************
	Returns true if all characters in string s are numbers.
 	Accepts non-signed integers only. 
*********************************************** */

var reInteger = /^\d+$/

function isInteger (s) {
	if (isEmpty (s))
		return false;
		   
 	return reInteger.test(s)
}


/* **********************************************
 	Returns true if all characters are numbers; 
	first character is allowed to be + or - as well.
*********************************************** */

var reSignedInteger = /^(\+|-)?\d+$/

function isSignedInteger (s) {   
	if (isEmpty (s))
		return false;
	
	return reSignedInteger.test(s)
}

/* **********************************************
	Returns true if string s is an integer > 0.
*********************************************** */
function isPositiveInteger (s) {   
	if (isEmpty (s))
		return false;
	
	return (isSignedInteger (s) && (parseInt (s) > 0));
}


/* ************************************************
	Returns true if string s is an integer >= 0.
************************************************* */

function isNonnegativeInteger (s) {   
	
	if (isEmpty (s))
		return false;
	
	return (isSignedInteger (s) && (parseInt (s) >= 0));
}


/* ************************************************
	Returns true if string s is an integer < 0.
************************************************* */

function isNegativeInteger (s) {   
	
	if (isEmpty (s))
		return false;
	
	return (isSignedInteger(s) && (parseInt (s) < 0) );
}


/* ************************************************
	Returns true if string s is an integer <= 0.
************************************************* */

function isNonpositiveInteger (s) {   
	
	if (isEmpty (s))
		return false;
	
	return (isSignedInteger (s) && (parseInt (s) <= 0));
}


/* ************************************************
	True if string s is an unsigned floating point (real) number. 
************************************************* */

var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;

function isFloat (s) {   
	if (isEmpty(s)) 
  	return false;
  
  return reFloat.test(s)
}


/* *************************************************
	True if string s is a signed or unsigned floating point 
	(real) number. First character is allowed to be + or -.
************************************************** */

var reSignedFloat = /^(((\+|-)?\d+(\.\d*)?)|((\+|-)?(\d*\.)?\d+))$/

function isSignedFloat (s) {   
	if (isEmpty(s)) 
  	return false;
  	
  return reSignedFloat.test(s)
}


/* ***************************************************
	Returns true if string s is English letters 
**************************************************** */

var reAlphabetic = /^[a-zA-Z]+$/

function isAlphabetic (s) {   
	if (isEmpty (s))
		return false;
	return reAlphabetic.test(s)
}


/* ***************************************************
	Returns true if string s is English letters 
 	(A .. Z, a..z) and numbers only.
**************************************************** */

var reAlphanumeric = /^[a-zA-Z0-9]+$/

function isAlphanumeric (s) {   
	
	if (isEmpty(s))
		return false;
	return reAlphanumeric.test(s)
}


/* ****************************************************
	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
***************************************************** */

var reEmail = /^.+\@.+\..+$/

function isEmail (s) {   
	if (isEmpty(s))
		return false;
	
	return reEmail.test(s)
}


/* *****************************************************
	Returns true if string s is a valid 
	Year number.  Must be 2 or 4 digits only.
****************************************************** */
function isYear (s) {
	if (isEmpty(s))
		return false;
	
	if (!isNonnegativeInteger (s)) 
		return false;
 	
	return ((s.length == 2) || (s.length == 4));
}


/* ******************************************************
 	Returns true if string s is an integer within the 
 	range of integer arguments a and b, inclusive.
	a < s < b
******************************************************* */

function isIntegerInRange (s, a, b) {   
	if (isEmpty(s))
		return false;
	
	if (!isInteger (s)) 
  	return false;

  var num = parseInt (s);
  return ((num >= a) && (num <= b));
}


/* ******************************************************
	Returns true if string s is a valid 
	month number between 1 and 12.
******************************************************* */
function isMonth (s) {   
	if (isEmpty(s))
		return false;
	
	return isIntegerInRange (s, 1, 12);
}


/* ******************************************************
	Returns true if string s is a valid 
	day number between 1 and 31.
******************************************************* */
function isDay (s) {   
	if (isEmpty(s))
		return false;
	
	return isIntegerInRange (s, 1, 31);
}


/* *******************************************************
	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 );
}



/* ********************************************************
	returns true if string arguments year, month, and day 
	form a valid date.
********************************************************* */ 

var daysInMonth = new Array (13);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function isDate (year, month, day) {   
	// catch invalid years (not 2- or 4-digit) and invalid months and days.
  if (! (isYear (year) && isMonth (month) && isDay(day))) 
  	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 ((intMonth != 2) && (intDay > daysInMonth[intMonth])) 
  	return false; 

	if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) 
		return false;
	
	return true;
}


/* ***************************************************
	Return true if the string of specified length.
**************************************************** */

function checkString (s, max, min, emptyOk) {
	
	if (isEmpty (s))
		return emptyOk;
	
	return (s.length >= min && s.length <= max);
}	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?