首頁  >  文章  >  web前端  >  頁用JavaScript驗證函數

頁用JavaScript驗證函數

高洛峰
高洛峰原創
2016-11-25 13:22:22880瀏覽

/************************************************** ******** ****** ************************************ *****/
/*************************************數字的驗證************ *****************************/
/************************************************** **** *************************************************/
/**
* 檢查輸入的一串字元是否全部是數字
* 輸入:str 字串
* 回傳:true 或 flase; true表示為數字
*/
function checkNum(str){
return str.match(/ D/) == null;
}

/**
* 檢查輸入的一串字元是否為小數
* 輸入:str 字串
* 傳回:true 或 flase; true表示為小數
*/
function checkDecimal(str){
if (str.match(/^-?d+(.d+)?$/g) == null ) {
回傳false;
}
else {
回傳true;
}
}
/**
* 檢查輸入的一串字元是否為整數資料
* 輸入:str 字串
* 傳回:true 或 flase; true表示為小數
*/
function checkInteger(str){
if (str.match(/^[-+]?d ) *$/) == null) {
回傳false;
}
else {
回傳true;
}
}
/************************************************** **** *************************************************/
/*************************************字元的驗證************ *****************************/
/************************************************** **** ********************************************** * **/

/**
* 檢查輸入的一串字元是否為字元
* 輸入:str 字串
* 回傳:true 或 flase; true表示為全部為字元 不包含漢字
*/
function checkStr(str){
if (/[^x00-xff]/g.test(str)) {
return false;
}
else {
return true;
}
}

/**
* 檢查輸入的一串字元是否包含漢字
* 輸入:str 字串
* 回傳:true 或 flase; true表示包含漢字
*/
function checkChinese(str){
if (escape(str).indexOf("%u") != -1) {
return true;
}
else {
return false;
}
}

/**
* 檢查輸入的郵箱格式是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
*/
function checkEmail(str){
if (str.match(/[A-Za-z0-9_-]+[@ ] (S*)(net|com|cn|org|cc|tv|[0-9]{1,3})(S*)/g) == null) {
回傳false;
}
else {
return true;
}
}

/**
* 檢查輸入的手機號碼格式是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
*/
function checkMobilePhone(str){
if (str.match(/^(?:13d|15[89])-?d{5} ( d{3}|*{3})$/) == null) {
回傳false;
}
else {
回傳true;
}
}

/**
* 檢查輸入的固定電話號碼是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
*/
function checkTelephone(str){
if (str.match(/^(([0+]d{2,3}-)?(0d{2,3})-) (d{7,8})(-(d{3,}))?$/) == null) {
return false;
}
else {
return true;
}
}
/**
* 檢查QQ的格式是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
*/
function checkQQ(str){
if (str.match(/^d{5,10}$/) == null) {
return false;
}
else {
return true;
}
}
else {
return true;
}
}
else {
return true;
}
}
else {
return true;
}
}
else {
return true;
}
}
else {
return true;
}
}
else {
return true;
}
}
/**
* 檢查輸入的身分證號是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
*/
function checkCard(str){
//15位數身分證正規表示式
var arg1 = /^[1-9]d{7}((0d)|(1[ 0-2]))(([0|1|2]d)|3[0-1])d{3}$/;
//18位數身分證正規表示式
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;
}
}
/**
* 檢查輸入的IP位址是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
*/
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;
}
}
/**
* 檢查輸入的URL位址是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
*/
function checkURL(str){
if (str.match(/(http[s]? ftp)://[^/.]+?..+w$/i) == null) {
return false

}🎜else {🎜return true;🎜}🎜}🎜/**🎜* 檢查輸入的字元是否具有特殊字元🎜* 輸入:str 字串🎜* 回傳:true 或 flase; true表示包含特殊字元🎜* 主要用於註冊資訊的時候驗證🎜*/ 🎜function checkQuote(str){🎜var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");🎜items.push(":", ";", "'", "|", "\", "", "?", "/", ">", "||", "//");🎜items.push("admin", "administrators", "administrator", "管理員", "系統管理員");🎜items.push("select", "delete", "update", "insert", "create", "drop", " alter", "trancate");🎜str = str.toLowerCase();🎜for (var i = 0; i = 0) {🎜return true;🎜}🎜}🎜return false;🎜}🎜

/************************************************** **** *************************************************/
/*************************************時間的驗證************ *****************************/
/************************************************** **** *************************************************/
/**
* 檢查日期格式是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
* 注意:此處無法驗證中文日期格式
* 驗證短日期(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) == 值[3] && date.getDate() = = value[4]);
}
}
/**
* 檢查時間格式是否正確
* 輸入:str 字串
* 回傳:true 或 flase; true表示格式正確
* 驗證時間(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 ||值[ 3]> 60||值[4]> 60) {
return false
}
else {
return true;
}
}
}
/**
* 檢查全日期時間格式是否正確
* 輸入:str 字串
* 檢查:true 或 flase; true表示格式正確
* (2007-06-05 10:57:10)
*/
function checkFull}
}
/**&*/
function checkFullTime(streckFull /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) == 值[3] && 日期。 getDate() == value[4] && date.getHours() == value[5] && date.getMinutes() == value[6] && date.getSeconds() == value[7]);

回傳 true ;

}

}🎜 🎜

/************************************************** **** *************************************************/
/************************************身分證號碼的驗證*********** **************************/
/************************************************** **** *************************************************/
/**
* 身分證15位元編碼規則:dddddd yymmdd xx p
* dddddd:地區碼
* yymmdd: 出生年月日
* xx: 順序類別編碼,無法確定
* p: 性別,奇數為男,偶數為女
*


* 身分證18位元編碼規則:dddddd yyyymmdd xxx y
* dddddd:地區碼
* yyyymmdd: 出生年月日
* xxx:順序類編碼,無法確定,奇數為男,偶數為女
* y: 校驗碼,該位數值可透過前17位計算獲得
*


* 18位號碼加權因子為(從右到左) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]
* 驗證位Y = [ 1, 0, 10, 9, 8, 7, 6, 5 , 4, 3, 2 ]
* 校驗位計算公式:Y_P = mod( ∑(Ai×Wi),11 )
* i為身分證號碼由右往左數的2...18 位元; Y_P為腳女校驗碼所在校驗碼數組位置
*
*/
var Wi = [7, 9, 10, 5, 8 , 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];// 加權因子
var ValideCode = [1, 0, 10, 9, 8, 7, 6 , 5, 4, 3, 2];// 身分證驗證位值.10代表X
function IdCardValidate(idCard){
idCard = trim(idCard.replace(/ /g, ""));
if (idCard .length == 15) {
return isValidityBrithBy15IdCard(idCard);
}
else
if (idCard.length == 18) {
var a_idCard = idCard.length == 18) {
var a_idCard = idCard.split(""); isValidityBrithBy18IdCard(idCard) && isTrueValidateCodeBy18IdCard(a_idCard)) {
return true;
}
else {
return false; */
function isTrueValidateCodeBy18IdCard(a_idCard){
var sum = 0; // 宣告加權求和變數
if (a_idCard[17].toLowerCase() == 'x') {
a_idCard[17] = 1098; x的驗證碼替換為10方便後續操作
}
for (var i = 0; i sum += Wi[i] * a_idCard[i];// 加權求和
}
valCodePosition = sum % 11;// 得到驗證碼所位置
if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
}
else {
return false#
}
}
else {
return false#
} &*/
function maleOrFemalByIdCard(idCard){
idCard = trim(idCard.replace(/ /g, ""));// 處理身分證號碼。包括字元間有空格。
if (idCard.length == 15) {
if (idCard.substring(14, 15) % 2 == 0) {
return 'female';
}
else {
return 'male';
else
if (idCard.length == 18) {
if (idCard.substring(14, 17) % 2 == 0) {
return 'female';
}
else {
return '
}
else {
return null;
}
}
/**
* 判斷身分證號碼為18位元時最後的驗證位是否正確
* @param a_idCard 身分證號碼陣列
* @return
*/
function isValidityBrithBy18IdCard(idCard18){
var year = idCard18.string(38.string); 10, 12);
var day = idCard18.substring(12, 14);
var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
///(getFull ,避免千年蟲問題
if (temp_date.getFullYear() != parseFloat(year) ||
temp_date.getMonth() != parseFloat(month) - 1 ||
temp_date.getDate() != parseate(Float) {
return false;
}
else {
return true;
}
}
/**
* 透過身分證判斷是男是女
* @param idCard 15/18位身分證號碼
* @return 'female'-女、'male'-男
*/
function isValidityBrithBy15IdCard(idCard15){
= idCard15.substring(8, 10);
var day = idCard15.substring(10, 12);
var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day);身分證中的你年齡則不需考慮千年蟲問題而使用getYear()方法
if (temp_date.getYear() != parseFloat(year) ||
temp_date.getMonth() != parseFloat(month) - 1 | |
temp_date.getDate() != parseFloat(day)) {
return false;
}
else {
return true;
}
}
//去掉字串頭尾註
str.replace(/(^s*)|(s*$)/g, "");
}



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn