Home >Web Front-end >JS Tutorial >Page JavaScript validation function
/**************************************************************************************/
/***************************************Verification of numbers************ *******************************/
/**************************************************************************************/
/**
* Check whether the input string of characters are all numbers
* Input: str string
* Return: true or false; true is expressed as a number
*/
function checkNum(str){
return str.match(/D/) == null;
}
/**
* Check whether the input string of characters is a decimal
* Input: str string
* Return: true or flase; true represents a decimal
*/
function checkDecimal(str){
if (str.match(/^-?d+(.d+)?$/g) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the input string of characters is integer data
* Input: str string
* Return: true or flase; true is expressed as a decimal
*/
function checkInteger(str){
if (str.match(/^[-+]?d*$/) == null) {
return false;
}
else {
return true;
}
}
/**************************************************************************************/
/***************************************Verification of characters************ *****************************/
/**************************************************************************************/
/**
* Check whether the entered string of characters is a character
* Input: str string
* Return: true or flase; true means all characters are characters and does not contain Chinese characters
*/
function checkStr(str){
if (/[^x00-xff]/g.test(str)) {
return false;
}
else {
return true;
}
}
/**
* Check whether the input string of characters contains Chinese characters
* Input: str string
* Return: true or false; true means it contains Chinese characters
*/
function checkChinese(str){
if (escape(str).indexOf("%u") != -1) {
return true;
}
else {
return false;
}
}
/**
* Check whether the entered email format is correct
* Input: str string
* Return: true or flase; true means the format is correct
*/
function checkEmail(str){
if (str.match(/[A-Za-z0-9_-]+[@](S*)(net|com|cn|org|cc|tv|[0-9]{1,3})(S*)/g) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the format of the entered mobile phone number is correct
* Input: str string
* Return: true or false; true means the format is correct
*/
function checkMobilePhone(str){
if (str.match(/^(?:13d|15[89])-?d{5}(d{3}|*{3})$/) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the entered fixed phone number is correct
* Input: str string
* Return: true or flase; true means the format is correct
*/
function checkTelephone(str){
if (str.match(/^(([0+]d{2,3}-)?(0d{2,3})-) (d{7,8})(-(d{3,}))?$/) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the format of QQ is correct
* Input: str string
* Return: true or flase; true means the format is correct
*/
function checkQQ(str){
if (str.match(/^d{5,10}$/) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the entered ID number is correct
* Input: str string
* Return: true or flase; true means the format is correct
*/
function checkCard(str){
//15-digit ID card regular expression
var arg1 = /^[1-9]d{7}((0d)|(1[ 0-2]))(([0|1|2]d)|3[0-1])d{3}$/;
//18-digit ID card regular expression
var arg2 = /^[ 1-9]d{5}[1-9]d{3}((0d)|(1[0-2]))(([0|1|2]d)|3[0-1]) ((d{4})|d{3}[A-Z])$/;
if (str.match(arg1) == null && str.match(arg2) == null) {
return false;
}
else {
return true;
}
}
/**
* Check whether the entered IP address is correct
* Input: str string
* Return: true or flase; true means the format is correct
*/
function checkIP(str){
var arg = /^(d{1,2}|1dd|2[0-4]d| 25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4] d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$/;
if (str.match(arg) == null ) {
return false;
}
else {
return true;
}
}
/**
* Check whether the entered URL address is correct
* Input: str string
* Return: true or flase; true means the format is correct
*/
function checkURL(str){
if (str.match(/(http[s]?| ftp)://[^/.]+?..+w$/i) == null) {
return false
}
else {
return true;
}
}
/**
* Check whether the entered characters have special characters
* Input: str string
* Return: true or flase; true means it contains special characters
* Mainly used for verification when registering information
*/
function checkQuote(str){
var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");
items.push(":", ";", "'", "|", "\", "<", ">", "?", "/", "<<", ">>", "||", "//");
items. push("admin", "administrators", "administrator", "administrator", "system administrator");
items.push("select", "delete", "update", "insert", "create" , "drop", "alter", "trancate");
str = str.toLowerCase();
for (var i = 0; i < items.length; i++) {
if (str.indexOf(items[ i]) >= 0) {
return true;
}
}
return false;
}
/**************************************************************************************/
/*************************************Time Verification************ *******************************/
/**************************************************************************************/
/**
* Check whether the date format is correct
* Input: str string
* Return: true or flase; true means the format is correct
* Note: Chinese date format cannot be verified here
* Verify short date (2007-06-05)
*/
function checkDate(str){
//var value=str.match(/((^((1[8-9]d{2})|([2-9]d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]d{2})|([2-9]d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]d{2})|([2-9]d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/);
var value = str.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2})$/);
if (value == null) {
return false;
}
else {
var date = new Date(value[1], value[3] - 1, value[4]);
return (date.getFullYear() == value[1] && (date.getMonth() + 1) == value[3] && date.getDate() == value[4]);
}
}
/**
* Check whether the time format is correct
* Input: str string
* Return: true or flase; true means the format is correct
* Verify time (10:57:10)
*/
function checkTime(str){
var value = str.match(/^(d{1,2})(:)?(d{1,2})2(d{1,2})$/)
if (value == null) {
return false;
}
else {
if (value[1] > 24 || value[3] > 60 || value[4] > 60) {
return false
}
else {
return true;
}
}
}
/**
* Check whether the full date and time format is correct
* Input: str string
* Return: true or flase; true means the format is correct
* (2007-06-05 10:57:10)
*/
function checkFullTime(str){
//var value = str.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2}) (d{1,2}):(d{1,2}):(d{1,2})$/);
var value = str.match(/^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/);
if (value == null) {
return false;
}
else {
//var date = new Date(checkFullTime[1], checkFullTime[3] - 1, checkFullTime[4], checkFullTime[5], checkFullTime[6], checkFullTime[7]);
//return (date.getFullYear() == value[1] && (date.getMonth() + 1) == value[3] && date.getDate() == value[4] && date.getHours() == value[5] && date.getMinutes() == value[6] && date.getSeconds() == value[7]);
return true;
}
}
/**************************************************************************************/
/**************************************Verification of ID number************ **************************/
/**************************************************************************************/
/**
* 15-digit coding rules for ID cards: dddddd yymmdd xx p
* dddddd: area code
* yymmdd: date of birth
* xx: sequential coding, cannot be determined
* p: gender, odd numbers are male, even numbers are female
*