Skip to content

Instantly share code, notes, and snippets.

@imbyc
Created May 21, 2020 08:47
Show Gist options
  • Save imbyc/c0cb4277d19aa7208b468c4350c43e1b to your computer and use it in GitHub Desktop.
Save imbyc/c0cb4277d19aa7208b468c4350c43e1b to your computer and use it in GitHub Desktop.
[js验证,包含多种验证] 包含验证和判断,从别的网站找到的 https://appoint.yihu.com/appoint/js/checkForm.js?v=20181211 #js #验证
/**验证Email**/
function isEmail(str) {
if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
return true;
else
return false
}
/**数字检查**/
function isNum(str) {
return str.match(/\D/) == null
}
/**检查是否为电话号码**/
function isNumber(String) {
var Letters = "1234567890-"; /**可以自己增加可输入值**/
var i;
var c;
if (String.charAt(0) == '-')
return false;
if (String.charAt(String.length - 1) == '-')
return false;
for (i = 0; i < String.length; i++) {
c = String.charAt(i);
if (Letters.indexOf(c) < 0)
return false;
}
return true;
}
/**中文值检测**/
function isChinese(name) {
if (name.length == 0)
return false;
for (i = 0; i < name.length; i++) {
if (name.charCodeAt(i) > 128)
return true;
}
return false;
}
/**英文值检测**/
function isEnglish(name) {
if (name.length == 0)
return false;
for (i = 0; i < name.length; i++) {
if (name.charCodeAt(i) > 128)
return false;
}
return true;
}
/**判断用户名是否为数字字母下滑线**/
function username(str) {
var reg = /[^A-Za-z0-9_]/g
if (reg.test(str)) {
return (false);
} else {
return (true);
}
}
/**判断网址**/
function IsURL(str_url) {
var re = new RegExp("((^http)|(^https)|(^ftp)):\/\/(\\w)+\.(\\w)+");
if (re.test(str_url)) {
return (true);
}
else {
return (false);
}
}
//验证手机号/开放所有号码段
function IsChinaMobile(value) {
/** 只验证第一位是1 **/
var res = /^[1]\d{10}$/;
var re = new RegExp(res);
if (re.test(value)) {
return true;
}
return false;
}
/**全角转换半角 电话号码格式转化**/
function CtoH(obj) {
var str = obj.value;
var result = "";
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) == 12288) {
result += String.fromCharCode(str.charCodeAt(i) - 12256);
continue;
}
if (str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375)
result += String.fromCharCode(str.charCodeAt(i) - 65248);
else result += String.fromCharCode(str.charCodeAt(i));
}
obj.value = result;
}
/**检测日期是否正确开始**/
function dateCheck(str) {
var re = new RegExp("^([0-9]{4})[.-]{1}([0-9]{1,2})[.-]{1}([0-9]{1,2})$");
var ar;
if ((ar = re.exec(str)) != null) {
var i;
i = parseFloat(ar[1]);
if (i <= 0 || i > 9999)
{ return false; }
i = parseFloat(ar[2]);
if (i <= 0 || i > 12)
{ return false; }
i = parseFloat(ar[3]);
if (i <= 0 || i > 31)
{ return false; }
}
else
{ return false; }
return true;
}
//判断身份证
function isIdCardNo(idcard) {
idcard = idcard.toLocaleUpperCase();
var Errors = new Array(
"验证通过!",
"身份证号码位数不对!",
"身份证号码出生日期超出范围或含有非法字符!",
"身份证号码校验错误!",
"身份证地区非法!"
);
var area = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" }
var idcard, Y, JYM;
var S, M;
var idcard_array = new Array();
idcard_array = idcard.split("");
/** 地区检验**/
if (area[parseInt(idcard.substr(0, 2))] == null) return false; /** Errors[4];
/** 身份号码位数及格式检验**/
switch (idcard.length) {
case 15:
if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0)) {
ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/; /** 测试出生日期的合法性**/
} else {
ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/; /** 测试出生日期的合法性**/
}
if (ereg.test(idcard)) {
//增加时间段判断
if (!CheckIdCardDate('19' + idcard.substr(6, 6)) && !CheckIdCardDate('20' + idcard.substr(6, 6))) {
return false;
}
return true; /** Errors[0];**/
}
else return false; /** Errors[2];**/
break;
case 18:
/** 18位身份号码检测**/
/** 出生日期的合法性检查**/
/** 闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))**/
/** 平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))**/
if (parseInt(idcard.substr(6, 4)) % 4 == 0 || (parseInt(idcard.substr(6, 4)) % 100 == 0 && parseInt(idcard.substr(6, 4)) % 4 == 0)) {
ereg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/; /** 闰年出生日期的合法性正则表达式**/
} else {
ereg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/; /** 平年出生日期的合法性正则表达式**/
}
if (ereg.test(idcard)) {/**测试出生日期的合法性**/
/** 计算校验位**/
S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
+ (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
+ (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
+ (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
+ (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
+ (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
+ (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
+ parseInt(idcard_array[7]) * 1
+ parseInt(idcard_array[8]) * 6
+ parseInt(idcard_array[9]) * 3;
Y = S % 11;
M = "F";
JYM = "10X98765432";
M = JYM.substr(Y, 1); /** 判断校验位**/
if (M == idcard_array[17]) {
//增加时间段判断
if (!CheckIdCardDate(idcard.substr(6, 8))) {
return false;
}
return true; /** Errors[0]; /**检测ID的校验位**/
}
else return false; /** Errors[3];**/
}
else return false; /** Errors[2];**/
break;
default:
return false; /** Errors[1];**/
break;
}
}
//验证出生日期范围
function CheckIdCardDate(dateStr)
{
//alert(dateStr);
try{
//dateStr = "20160808"; //"800101";
var dateNow = new Date();
var dateMin = new Date(dateNow.getYear() - 120, dateNow.getMonth(), dateNow.getDate()); //120年
var dateMax = dateNow; // 0天 //new Date(dateNow - 86400000 * 30);//30天
var dateInp = new Date(parseInt(dateStr.substr(0, 4)), parseInt(dateStr.substr(4, 2))-1, parseInt(dateStr.substr(6, 2)));
}
catch(ex){
return true;
}
if (dateInp >= dateMin && dateInp <= dateMax)
{
return true;
}
return false; // - dateInp;
}
//非法字符过滤
function stripscript(s) {
var pattern = new RegExp("[`~!@$^&*()=|{}':;'\",\\\\[\\].<>/?~!@¥……&*()—|{}【】‘;:”“'。,、?]");
var rs = "";
for (var i = 0; i < s.length; i++) {
var testStr = s.substr(i, 1);
if (pattern.test(testStr)) {
rs += testStr + " ";
}
//alert("testStr:" + testStr + " | " + pattern.test(testStr));
}
return rs;
}
//是否包含中文
function ContainChinese(str) {
if (/.*[\u4e00-\u9fa5]+.*$/.test(str)) {
return true;
} else {
return false;
}
}
/*计算年龄*/
function AgeCalculation(birthday) {
if (birthday == null || birthday == "") {
return 0;
}
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
var memberyear = birthday.substr(0, 4);
var membermonth = birthday.substr(5, 2);
var memberday = birthday.substr(8, 2);
var age = myDate.getFullYear() - memberyear - 1;
if (membermonth < month || membermonth == month && memberday <= day) {
age++;
}
return isNaN(age)?0:age;
}
//通过身份证获取生日
function GetBirthdayByIdcard(idcard)
{
if (idcard.length == 18) {
return idcard.substring(6, 10) + "-" + idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
}
else if (idcard.length == 15) {
return '19' + idcard.substring(6, 8) + "-" + idcard.substring(8, 10) + "-" + idcard.substring(10, 12);
}
return "";
}
//根据身份证计算年龄
function GetAgeByIdcard(uUserCard) {
var memberBirthday = "";//1900-01-01";
//获取出生日期
if (uUserCard.length == 18) {
memberBirthday = uUserCard.substring(6, 10) + "-" + uUserCard.substring(10, 12) + "-" + uUserCard.substring(12, 14);
} else if (uUserCard.length == 15) {
memberBirthday = "19" + uUserCard.substring(6, 8) + "-" + uUserCard.substring(8, 10) + "-" + uUserCard.substring(10, 12);
}
var age = AgeCalculation(memberBirthday);
//获取性别
if (parseInt(uUserCard.substr(16, 1)) % 2 == 1) {
//男
memberSexId = 1;
} else {
//女
memberSexId = 2;
}
return age;
}
//通过身份证获取性别
function GetSexByIdcard(idcard){
var memberSexId = 1;
if (uUserCard.length == 18 && parseInt(uUserCard.substr(16, 1)) % 2 == 0) {
memberSexId = 2;
}
else if (uUserCard.length == 15 && parseInt(uUserCard.substr(13, 1)) % 2 == 0) {
memberSexId = 2;
}
return memberSexId;
}
//获取星期riqi 2018-01-01
function GetWeek(riqi) {
var weeks = ["日", "一", "二", "三", "四", "五", "六"];
var arys1 = riqi.split('-'); //日期为输入日期,格式为 2013-3-10
var ssdate = new Date(arys1[0], parseInt(arys1[1] - 1), arys1[2]);
return weeks[ssdate.getDay()]; //就是你要的星期几
}
//获取星期 day 0-6
function GetWeekByDay(day){
try{
var weeks = ["日", "一", "二", "三", "四", "五", "六"];
return weeks[parseInt(day)];
}
catch(e){
return "";
}
}
//获取上下午
function GetTimeName(timeId){
if(timeId==null || timeId==''){
return "";
}
try{
var itemNaArr = new Array('', '上午', '下午', '晚上');
return itemNaArr[timeId];
}
catch(e){
return "";
}
}
//分格式化 1200 to 12.00
function FomatFeeFeng(fee) {
return (parseFloat(fee) / 100).toFixed(2);
}
//分格式化 1200 to 12 / 1230 to 12.30
function FomatFeeFeng2(fee) {
if (parseInt(fee) % 100 == 0) {
return (parseInt(fee) / 100);
}
return (parseFloat(fee) / 100).toFixed(2);
}
function FomatFeeFeng3(fee) {
if (parseInt(fee) % 100 == 0) {
return (parseInt(fee) / 100);
}
if (parseInt(fee) % 10== 0) {
return (parseInt(fee) / 100).toFixed(1);
}
return (parseFloat(fee) / 100).toFixed(2);
}
//元格式化 12 to 12.00
function FomatFeeYuan(fee) {
return parseFloat(fee).toFixed(2);
}
//验证各种证件
//中国护照:
//1)DE、SE、PE+7位数字编码
//2)E+8位数字编码
//3)D、S、P、G+8位数字编码
//国外护照:至少4位数字或字母(支持数字与字母混搭)
//港澳身份证:
//1)香港:1-2位字母 + 6位数字编码 + (数字/字母A)
//2)澳门:1/5/7 + 6位数字编码 + (数字/字母A)
//台湾身份证:字母 + 1/2 + 8位数字编码
//婴幼儿出生证:字母 + 9位数字编码
function CheckCert(cerNo, cerType) {
if (cerNo != null && cerNo != "") {
cerNo = cerNo.toUpperCase();
var reg;
switch (String(cerType)) {
case "1": //身份证
if (cerNo.indexOf('*') > -1) {
return true;
}
return isIdCardNo(cerNo);
case "3": //中国护照
reg = /^((DE|SE|PE)\d{7}|(E|D|S|P|G)\d{8})$/;
return reg.test(cerNo);
case "8": //国外护照
reg = /^[0-9A-Z]{4,}$/;
return reg.test(cerNo);
case "9": //港澳身份证
reg = /^([A-Z]{1,2}\d{6}\([0-9A]\)|(1|5|7)\d{6}\([0-9A]\))$/;
return reg.test(cerNo);
case "10": //婴幼儿出生证
reg = /^[A-Z]\d{9}$/;
return reg.test(cerNo);
case "11": //台湾身份证
reg = /^[A-Z](1|2)\d{8}$/;
return reg.test(cerNo);
case "12": //港澳台居住证
reg = /^\d{18}$/;
return reg.test(cerNo);
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment