///引用方法,直接将该gldvld.js引用到页面的底部即可,不要引用css,会自动引入该css
//spvld 是specialValidate的缩写
/////全局声明
var spvld = window.NameSpace || {};
/////js获取到在身所在文件的路径,然后拼出所需css的路径,放到引用文件的头部
spvld.getRootPath = function () {
var getJsUrl = document.scripts;
getJsUrl = getJsUrl[getJsUrl.length - 1].src.substring(0, getJsUrl[getJsUrl.length - 1].src.lastIndexOf("/") + 1);
var cssHref = getJsUrl + "spvld.css";
$("head").append("");
}
spvld.getRootPath();
var lblError = "";
$(document).ready(function () {
spvld.getValidate();
});
//将验证引入到方法名
spvld.getValidate = function () {
spvld.require(); //验证
spvld.requireDate();//日期必填
spvld.telephone();
spvld.phone();
spvld.tel();
spvld.IDcard();
spvld.email();
spvld.postcode();
spvld.fax();
spvld.num();
spvld.decimal();
spvld.url();
spvld.ip();
spvld.chinese();
spvld.username();
spvld.pwd();
}
// 表单验证,利用class名来获取对象
spvld.lblError = function (obj) {
if ($(obj).parent().children(".sp-error").length < 1) { //$(obj).parent().html().indexOf("sp-error") == -1
$(obj).after(lblError);
$(obj).parent().addClass('sp-text-nowrap').addClass("sp-relative");
}
}
spvld.getblur = function (classN, regex, tip) {
var className = $(classN).attr('class');
var str = $.trim($(classN).val());
var strReg = !!str.match(regex);
if (str != "" && strReg == false) {
$(classN).next("label").show().html(tip);
$(classN).removeClass("sp-rightLogo").addClass('sp-errorTip').addClass('sp-errorLogo');
$(classN).focus(); return false;
}
else if (str != "" && strReg == true) {
$(classN).removeClass('sp-errorTip').removeClass('sp-errorLogo').addClass("sp-rightLogo");
$(classN).next("label").hide().html("");
}
if (className.indexOf('sp-required') == -1) {
if (str == "") {
$(classN).removeClass('sp-errorTip').removeClass('sp-errorLogo');
$(classN).next("label").hide().html("");
}
}
}
//1.不能为空 class="sp-required"
spvld.require = function () {
$(".sp-required").each(function () {
$(this).addClass('sp-requireLogo');
spvld.lblError('.sp-required');
$(this).blur(function () {
var newVal = $.trim($(this).val());
if (newVal == "") {
$(this).next("label").show().html("必填");
$(this).addClass('sp-errorTip').removeClass('sp-requireLogo').removeClass('sp-rightLogo').addClass('sp-errorLogo');
}
else {
$(this).next("label").hide().html("");
$(this).removeClass('sp-errorTip').removeClass('sp-errorLogo');
}
});
});
}
//1.2. 日期验证不能为空 class="sp-required-date"
spvld.requireDate = function () {
$(".sp-required-date").each(function () {
$(this).addClass('sp-requireLogo');
spvld.lblError('.sp-required-date');
//$(this).blur(function () {
// var get_id = $(this).attr("id");
// var newVal = $.trim($("#" + get_id).val());
// if (newVal != "" && newVal != null) {
// $("#" + get_id).next("label").hide().html("");
// $("#" + get_id).removeClass('sp-errorTip').removeClass('sp-errorLogo');
// }
//});
});
}
//2.验证电话号码:手机+座机 class="sp-telephone"
spvld.telephone = function () {
$('.sp-telephone').each(function () {
spvld.lblError('.sp-telephone');
$(this).blur(function (event) {
var tel = $.trim($(this).val());
if (tel.substring(0, 1) == 1) {
var regex = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
spvld.getblur(this, regex, "手机号错误");
}
else {
var regex = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
spvld.getblur(this, regex, "座机号错误");
}
});
});
}
//2.2验证手机号码 class="sp-phone"
spvld.phone = function () {
$('.sp-phone').each(function () {
spvld.lblError('.sp-phone');
$(this).blur(function (event) {
var regex = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
spvld.getblur(this, regex, "手机号错误");
});
});
}
//2.3验证座机号码
spvld.tel = function () {
$('.sp-tel').each(function () {
spvld.lblError('.sp-tel');
$(this).blur(function (event) {
var regex = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
spvld.getblur(this, regex, "座机号错误");
});
});
}
//3.验证身份证号 class="sp-card"
spvld.IDcard = function () {
$('.sp-card').each(function () {
spvld.lblError('.sp-card');
$(this).blur(function () {
var regex = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
spvld.getblur(this, regex, "身份证错误");
});
});
}
//4.验证邮箱
spvld.email = function () {
$('.sp-email').each(function () {
spvld.lblError('.sp-email');
$(this).blur(function () {
var regex = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
spvld.getblur(this, regex, "邮箱错误");
});
});
}
//5.邮编 postcode
spvld.postcode = function () {
$('.sp-postcode').each(function () {
spvld.lblError('.sp-postcode');
$(this).blur(function () {
var regex = /^[0-9][0-9]{5}$/;
spvld.getblur(this, regex, "邮编错误");
});
});
}
//6.传真 fax
spvld.fax = function () {
$('.sp-fax').each(function () {
spvld.lblError('.sp-fax');
$(this).blur(function () {
var regex = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
spvld.getblur(this, regex, "传真号错误");
});
});
}
//7.只允许数字 num
spvld.num = function () {
$('.sp-num').each(function () {
spvld.lblError('.sp-num');
$(this).blur(function () {
var regex = /^[0-9]*$/;
spvld.getblur(this, regex, "输入数字");
});
});
}
//7_2.数字和小数 decimal
spvld.decimal = function () {
$('.sp-decimal').each(function () {
spvld.lblError('.sp-decimal');
$(this).blur(function () {
var regex = /^\d+(\.\d+)?$/;
spvld.getblur(this, regex, "请输入数字或小数");
});
});
}
//8.网址 url
spvld.url = function () {
$('.sp-url').each(function () {
spvld.lblError('.sp-url');
$(this).blur(function () {
var regex = /((https|http|ftp|rtsp|mms):\/\/)?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)/g;
spvld.getblur(this, regex, "网址错误");
});
});
}
//9.ip地址 ip
spvld.ip = function () {
$('.sp-ip').each(function () {
spvld.lblError('.sp-ip');
$(this).blur(function () {
var regex = /^\d+\.\d+\.\d+\.\d+$/;
spvld.getblur(this, regex, "ip地址错误");
});
});
}
//10.匹配中文 chinese
spvld.chinese = function () {
$('.sp-chinese').each(function () {
spvld.lblError('.sp-chinese');
$(this).blur(function () {
var regex = /^[\u4E00-\u9FFF]+$/;
spvld.getblur(this, regex, "请输入中文汉字");
});
});
}
//11.验证用户名合法 username
spvld.username = function () {
$('.sp-username').each(function () {
spvld.lblError('.sp-username');
$(this).blur(function () {
var regex = /^(?![^a-zA-Z]+$)(?!\D+$).{5,20}$/;
spvld.getblur(this, regex, "用户名必须由5-20位字母、数字组成");
});
});
}
//12.验证密码 pwd
var spanpwd = "";
spvld.pwd = function () {
$('.sp-password').each(function () {
var className = $(this).attr('class');
spvld.lblError('.sp-password');
if ($(this).parent().children(".sp-strength-tip").length < 1) { //$(obj).parent().html().indexOf("sp-error") == -1
$(this).next("label").after(spanpwd);
}
$(this).blur(function () {
$(".sp-strength-tip").hide();
$(this).removeClass("sp-rightLogo");
var pwd = $.trim($(this).val());
var pwdReg = !!pwd.match(/^(?![^a-zA-Z]+$)(?!\D+$).{6,30}$/);//字母数字
var pwdRegCheck = !!pwd.match(/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,.\/]).{6,30}$/);//字母数字特殊符号
/////var pwdReg = !!get_password.match(/^[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{6,30}$/);//同时包含大写字母、小写字母、数字、特殊字符
if (pwd != "") {
if (pwdReg == false) {
$(this).next("label").addClass("arrow-top").show().html("密码必须由6-30位字母、数字组成");
$(this).removeClass("sp-rightLogo").addClass('sp-errorTip'); $(this).focus();
return false;
} else {
$(this).removeClass('sp-errorTip').removeClass('sp-errorLogo').addClass("sp-rightLogo");
$(this).next("label").hide().html("");
if (pwd.length < 9) {
$(this).parent().find(".sp-strength-tip").show().text("强度低");
} else {
$(this).parent().find(".sp-strength-tip").show().text("强度中");
}
}
//若含有特殊字符则表示 强度高
if (pwdRegCheck == true && pwd.length > 9) {
$(this).removeClass('sp-errorTip').removeClass('sp-errorLogo').addClass("sp-rightLogo");
$(this).next("label").hide().html("");
$(this).parent().find(".sp-strength-tip").show().text("强度高");
}
}
if (className.indexOf('sp-required') == -1) {
if (pwd == "") { $(this).removeClass('sp-errorTip'); $(this).next("label").hide().html(""); }
}
});
});
}
//13.提交按钮点击时检测 用法如下:
//$("#Save").click(function () {
// if (spvld.submitCheck("#validateTest")) {
// 这里写要操作的代码后台
// }
//});
spvld.submit = function (formID) {
/// 提交时检测form表单格式的合法性
var result = true;
$(formID + " label.sp-error").each(function () {
var getClass = $(this).prev().attr("class"); //获取当前节点的前一个节点
if (getClass.indexOf("sp-required") != -1) { //如果含有sp-required必填
//含有必填,其值为空时
if ($(this).prev().val() == "" || $(this).prev().val() == null) {
if ($(this).is(":hidden")) {
$(this).show().html("必填");
$(this).prev().addClass('sp-errorTip').removeClass('sp-requireLogo').addClass('sp-errorLogo');
}
result = false;
}
if ($(this).prev().val() != "") {
if ($(this).is(":visible")) {
result = false;
}
}
}
//如果不是必填,但含有其他验证 时
if (getClass.indexOf("sp-required") == -1) {
if ($(this).prev().val() != "") {
if ($(this).is(":visible")) {
result = false;
}
}
}
});
////日期控件的必填检测
$(formID + " .sp-required-date").each(function () {
if ($(this).val() == "" || $(this).val() == null) {
///让其后面的
$(this).next("label").show().html("必填");
$(this).addClass('sp-errorTip').removeClass('sp-requireLogo').addClass('sp-errorLogo');
result = false;
///权宜之计是给时间验证加上延时然后去掉提示样式
setTimeout(function () {
$(formID + " .sp-required-date").next("label").show().html("");
$(formID + " .sp-required-date").removeClass('sp-errorTip').addClass('sp-requireLogo').removeClass('sp-errorLogo');
}, 3000);
}
});
///$(formID + " .sp-error:visible:eq(0)").prev().focus(); //将焦点定位到第一个sp-error所属的文本框中
return result;
}
//14.自定义方法来检测重复
//调用时必须放在$(document).ready(function () {});里面,否则会因在dom之前加载未找到节点而导致报错。
spvld.vldrepeat = function (classN, funMethod, tip) {
/// 自主检测
$(classN).each(function () {
var className = $(this).attr('class');
$(this).blur(function () {
spvld.lblError(this);
var getVal = $.trim($(this).val());
var funResult = funMethod(); //接收返回结果
//如果包含必填class
if (className.indexOf('sp-required') != -1) {
if (getVal != "" && funResult == false) {
$(this).next("label").show().html(tip);
$(this).removeClass("sp-rightLogo").addClass('sp-errorTip').addClass('sp-errorLogo');
$(this).focus(); return false;
}
else if (getVal != "" && $(this).next("label").is(":hidden")) {
$(this).removeClass('sp-errorTip').removeClass('sp-errorLogo').addClass("sp-rightLogo");
$(this).next("label").hide().html("");
}
}
//如果没有必填class
if (className.indexOf('sp-required') == -1) {
if (getVal != "" && funResult == false) {
$(this).next("label").show().html(tip);
$(this).removeClass("sp-rightLogo").addClass('sp-errorTip').addClass('sp-errorLogo');
$(this).focus(); return false;
} else if (getVal == "") {
$(this).removeClass('sp-errorTip').removeClass('sp-errorLogo');
$(this).next("label").hide().html("");
}
}
});
});
}
//15.判断两次密码是否一致
spvld.vldpwd = function (createPwd, confirmPwd) {
$(confirmPwd).blur(function () {
var className = $(this).attr('class');
spvld.lblError(this);
var getcreatePwd = $.trim($(createPwd).val());
var getconfirmPwd = $.trim($(this).val());
//如果包含必填class
if (className.indexOf('sp-required') != -1) {
if (getcreatePwd != "" && getconfirmPwd != "" && getcreatePwd != getconfirmPwd && $(this).next("label").is(":hidden")) {
$(this).next("label").show().html("两次输入密码不一致");
$(this).removeClass("sp-rightLogo").addClass('sp-errorTip').addClass('sp-errorLogo');
$(this).focus();
return false;
}
}
//如果没有必填class
if (className.indexOf('sp-required') == -1) {
if (getcreatePwd != "" && getconfirmPwd != "" && getcreatePwd != getconfirmPwd) {
$(this).next("label").show().html("两次输入密码不一致");
$(this).removeClass("sp-rightLogo").addClass('sp-errorTip').addClass('sp-errorLogo');
$(this).focus();
return false;
} else if (getconfirmPwd == "") {
$(this).removeClass('sp-errorTip').removeClass('sp-errorLogo');
$(this).next("label").hide().html("");
}
}
});
}
//成功时清除掉“对号”图片logo
spvld.clear = function (formID) {
$(formID + " .sp-rightLogo").each(function () {
$(this).removeClass("sp-rightLogo");
$(this).next("label").hide();
});
$(formID + " .sp-required").each(function () {
$(this).removeClass("sp-errorTip").removeClass("sp-errorLogo").addClass("sp-requireLogo");
$(this).next("label").hide();
});
}