我正在对焦点事件进行一些验证现在我想在按钮点击事件上检查所有这些验证我该怎么做
$("#ContentPlaceHolder1_txtPassword").focusout(function () {
var Password = $("#ContentPlaceHolder1_txtPassword").val();
if (Password == '') {
$("#ContentPlaceHolder1_txtPassword").css('border-color', 'red');
$("#ContentPlaceHolder1_spPassword").css('visibility', 'visible');
}
else if (Password.length < 8) {
$("#ContentPlaceHolder1_txtPassword").css('border-color', 'red');
$("#ContentPlaceHolder1_spPassword").css('visibility', 'visible');
$("#ContentPlaceHolder1_spPassword").text("Password should be atleast 8 characters ")
}
else {
$("#ContentPlaceHolder1_txtPassword").css('border-color', '');
$("#ContentPlaceHolder1_spPassword").css('visibility', 'hidden');
}
});
答案 0 :(得分:0)
$('#yourbuttonid').on("click" , function() {
//your existing above logic comes here
});
答案 1 :(得分:0)
重复的代码应该是function
,因此请定义validation
function
:
function validation() {
var Password = $("#ContentPlaceHolder1_txtPassword").val();
if (Password == '') {
$("#ContentPlaceHolder1_txtPassword").css('border-color', 'red');
$("#ContentPlaceHolder1_spPassword").css('visibility', 'visible');
}
else if (Password.length < 8) {
$("#ContentPlaceHolder1_txtPassword").css('border-color', 'red');
$("#ContentPlaceHolder1_spPassword").css('visibility', 'visible');
$("#ContentPlaceHolder1_spPassword").text("Password should be atleast 8 characters ")
}
else {
$("#ContentPlaceHolder1_txtPassword").css('border-color', '');
$("#ContentPlaceHolder1_spPassword").css('visibility', 'hidden');
}
}
然后,您在事件中调用此function
:
$("#ContentPlaceHolder1_txtPassword").focusout(function () {
validation();
});
$("#myButton").click(function() {
validation();
});