常用的表单验证js

时间:2021-12-13 06:14:06


JS验证

* js各种表单数据验证
*/

function checkNum(str){
return str.match(/\D/) == null;
}



function checkDecimal(str){
if (str.match(/^-?\d+(\.\d+)?/g) == null) {
return false;
}
else {
return true;
}
}


function checkInteger(str){
if (str.match(/^[-+]?\d*/) == null) {
return false;
}
else {
return true;
}
}


function checkStr(str){
if (/[^\x00-\xff]/g.test(str)) {
return false;
}
else {
return true;
}
}



function checkChinese(str){
if (escape(str).indexOf("%u") != -1) {
return true;
}
else {
return false;
}
}



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;
}
}



function checkMobilePhone(str){
if (str.match(/^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})/) == null) {
return false;
}
else {
return true;
}
}



function checkTelephone(str){
if (str.match(/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?/) == null) {
return false;
}
else {
return true;
}
}


function checkQQ(str){
if (str.match(/^\d{5,10}/) == null) {
return false;
}
else {
return true;
}
}


function checkCard(str){
//15位数身份证正则表达式
var arg1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}/;
//18位数身份证正则表达式
var arg2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(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;
}
}


function checkIP(str){
var arg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])/;
if (str.match(arg) == null) {
return false;
}
else {
return true;
}
}


function checkURL(str){
if (str.match(/(http[s]?|ftp):\/\/[^\/\.]+?\..+\w/i) == null) {
return false
}
else {
return 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 < items.length; i++) {
if (str.indexOf(items[i]) >= 0) {
return true;
}
}
return false;
}



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]);
}
}


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;
}
}
}


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;
}

}




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.split("");// 得到身份证数组
if (isValidityBrithBy18IdCard(idCard) && isTrueValidateCodeBy18IdCard(a_idCard)) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}


function isTrueValidateCodeBy18IdCard(a_idCard){
var sum = 0; // 声明加权求和变量
if (a_idCard[17].toLowerCase() == 'x') {
a_idCard[17] = 10;// 将最后位为x的验证码替换为10方便后续操作
}
for (var i = 0; i < 17; i++) {
sum += Wi[i] * a_idCard[i];// 加权求和
}
valCodePosition = sum % 11;// 得到验证码所位置
if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
}
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 'male';
}
}
else {
return null;
}
}


function isValidityBrithBy18IdCard(idCard18){
var year = idCard18.substring(6, 10);
var month = idCard18.substring(10, 12);
var day = idCard18.substring(12, 14);
var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
// 这里用getFullYear()获取年份,避免千年虫问题
if (temp_date.getFullYear() != parseFloat(year) ||
temp_date.getMonth() != parseFloat(month) - 1 ||
temp_date.getDate() != parseFloat(day)) {
return false;
}
else {
return true;
}
}


function isValidityBrithBy15IdCard(idCard15){
var year = idCard15.substring(6, 8);
var month = 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;
}
}

//去掉字符串头尾空格
function trim(str){
return str.replace(/(^\s*)|(\s*)/g, "");
}