HTML/Html-practice/jQuery/js/FormValidation.js
2023-05-01 22:47:22 +08:00

97 lines
2.7 KiB
JavaScript

;
(function ($) {
var methods = {
//用户名验证
userName: function () {
// 定义默认字段
var success = "success";
var error = "Error";
var info = "UserName";
// 用户名验证的正则表达式
var reg = /^[a-zA-Z0-9\_\+\-\&\$\*]{6,10}$/;
// 用户名为空的情况
if (this.val() == '') {
// $(".info1").css('color','#000');
$(".info1").show().html(info);
return false;
} else if (this.val().match(reg)) { // 用户名正则匹配
$(".info1").css('color', 'green');
// $(".info1").css('background','#fff');
$(".info1").show().html(success);
return true;
} else { // 正则匹配失败触发的事件
$(".info1").css('color', 'red');
$(".info1").show().html(error);
return false;
}
},
// 密码验证
password: function () {
// 定义默认字段
var success = "success";
var error = "Error";
var info = "Password";
// 密码验证正则表达式
var reg = /^[a-zA-Z0-9\_\-]{8,10}$/;
// 密码为空的情况
if (this.val() == '') {
// $(".info1").css('color','#000');
$(".info3").show().html(info);
return false;
} else if (this.val().match(reg)) { // 正则匹配
$(".info3").css('color', 'green');
$(".info3").show().html(success);
return true;
} else { // 匹配失败的情况
$(".info3").css('color', 'red');
$(".info3").show().html(error);
return false;
}
},
//邮箱验证
email: function () {
// 定义默认字段
var success = "success";
var error = "Error";
var info = "Email";
// 邮箱验证正则表达式
var reg = /^\w+@\w+(.[a-zA-Z]{2,3}){1,2}$/;
// 邮箱为空的情况
if (this.val() == '') {
// $(".info1").css('color','#000');
$(".info2").show().html(info);
return false;
} else if (this.val().match(reg)) { // 正则匹配
$(".info2").css('color', 'green');
$(".info2").show().html(success);
return true;
} else { // 匹配失败事件
$(".info2").css('color', 'red');
$(".info2").show().html(error);
return false;
}
}
};
// 函数调用的方法
$.fn.tooltip = function (method) {
// Method calling logic
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.tooltip');
}
};
})(jQuery);