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

91 lines
2.3 KiB
JavaScript

;
(function ($) {
var methods = {
//用户名验证
userName: function () {
var success = "success";
var error = "Error";
var info = "UserName";
//这些方法里面的this指针已经被替换为了$(this)
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);