Javascript 如果文本框为空,则在选项卡上按“抛出错误”
我有注册屏幕,其中有用户名,密码,验证码,电子邮件id等。,。我需要在jquery中进行验证。验证是,如果在用户名文本框中按Tab键而不输入任何值,则会向用户抛出错误消息或一些信息,说明用户名不应为空。我是一个新的jquery,做了几次验证,但它不能正常工作。有什么建议吗?这是在MVC cshtml页面中 MVC cshtml页面Javascript 如果文本框为空,则在选项卡上按“抛出错误”,javascript,jquery,asp.net-mvc-3,Javascript,Jquery,Asp.net Mvc 3,我有注册屏幕,其中有用户名,密码,验证码,电子邮件id等。,。我需要在jquery中进行验证。验证是,如果在用户名文本框中按Tab键而不输入任何值,则会向用户抛出错误消息或一些信息,说明用户名不应为空。我是一个新的jquery,做了几次验证,但它不能正常工作。有什么建议吗?这是在MVC cshtml页面中 MVC cshtml页面 @Html.TextBoxFor(m => m.Referenence_Cd, new Dictionary<string, object> { {
@Html.TextBoxFor(m => m.Referenence_Cd, new Dictionary<string, object> { { "id", "TxtReferenence_Cd" }, { "name", "Referenence_Cd" }, { "Class", "textBoxPlacHold" }, { "spellCheck", false }, { "maxLength", "13" } })
@Html.TextBoxFor(m => m.Authentication_Cd, new Dictionary<string, object> {{ "id", "TxtAuthentication_Cd" }, { "name", "Authentication_Cd" }, { "Class", "textBoxPlacHold" } ,{"spellCheck",false}, { "maxLength", "12" }})
@Html.TextBoxFor(m => m.Login_Id, new Dictionary<string, object> {{ "id", "Txtlogin" }, { "name", "Login_Id" }, { "Class", "textBoxPlacHold" }, { "jsValidation", "checkEMailAddr" } })
@Html.PasswordFor(m => m.Password, new Dictionary<string, object> {{ "id", "txtPassword" }, { "name", "Password" }, { "Class", "textBoxPlacHold txtPassword" } })
Jquery:
$('#TxtReferenence_Cd').blur(function () {
if ($('#TxtReferenence_Cd').val() == "") {
$('#spnVerificationErr').show();
$('#spnVerificationErr').html("Reference number should not be empty!");
}
else{
$('#spnVerificationErr').hide();
$('#spnVerificationErr').html("");
@Html.TextBoxFor(m=>m.referencence_Cd,新字典{{“id”,“txtferencence_Cd”},{“name”,“referencence_Cd”},{“Class”,“textboxplachhold”},{“拼写检查”,false},{“maxLength”,“13”})
@TextBoxFor(m=>m.Authentication_Cd,新字典{{{“id”,“TxtAuthentication_Cd”},{“name”,“Authentication_Cd”},{“Class”,“textboxplachhold”},{“拼写检查”,false},{“maxLength”,“12”})
@TextBoxFor(m=>m.Login\u Id,新字典{{{“Id”,“Txtlogin”},{“name”,“Login\u Id”},{“Class”,“textboxplachhold”},{“jsValidation”,“checkEMailAddr”})
@Html.PasswordFor(m=>m.Password,新字典{{“id”,“txtPassword”},{“name”,“Password”},{“Class”,“textboxplachhold txtPassword”})
Jquery:
$('#txtReference_Cd').blur(函数(){
if($('#txtReference_Cd').val()=“”){
$('#spnVerificationErr').show();
$('#spnVerificationErr').html(“参考号不应为空!”);
}
否则{
$('#spnVerificationErr').hide();
$('#spnVerificationErr').html(“”);
按tab键时需要显示验证消息,如果我按tab键而不输入任何值,则应显示参考号不应为空
$(document).on('keydown', 'input', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
e.preventDefault();
// show the message here
}
});
更新:
您必须能够在自己的代码中实现它,但无论如何:
Jquery:
$(document).on('keydown','#TxtReferenence_Cd',function (e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
if ($(this).val() == "") {
e.preventDefault();
$('#spnVerificationErr').show();
$('#spnVerificationErr').html("Reference number should not be empty!");
}
else{
$('#spnVerificationErr').hide();
$('#spnVerificationErr').html("");
}
}
});
我自己已经找到了答案..贴在这里是为了帮助其他人
$('#TxtReferenence_Cd').blur(function () {
if ($('#TxtReferenence_Cd').val() == '') {
$('#TxtReferenence_Cd').addClass('outLineRed');
$('#spnVerificationErr').show();
$('#spnVerificationErr').html("Reference number should not empty!");
$('#MsgSucc').attr("style", "background-color:White;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#spnVerificationErr').hide();
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
$('#TxtReferenence_Cd').removeClass('outLineRed');
}
}
);
$('#Authentication_Cd').blur(function () {
if ($('#Authentication_Cd').val() == '') {
$('#Authentication_Cd').addClass('outLineRed');
$('#spnVerificationErr').show();
$('#spnVerificationErr').html("Reference number should not empty!");
$('#MsgSucc').attr("style", "background-color:White;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#spnVerificationErr').hide();
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
$('#Authentication_Cd').removeClass('outLineRed');
}
}
);
$('#Txtlogin').blur(function () {
if ($('#Txtlogin').val() == '') {
$('#Txtlogin').addClass('outLineRed');
$('#spnVerificationErr').show();
$('#spnVerificationErr').html("Email address should not be empty!");
$('#MsgSucc').attr("style", "background-color:White;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#spnVerificationErr').hide();
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
$('#Txtlogin').removeClass('outLineRed');
}
}
);
$('#txtPassword').blur(function () {
if ($('#txtPassword').val() == '') {
$('#txtPassword').addClass('outLineRed');
$('#spnVerificationErr').show();
$('#spnVerificationErr').html("Password should not be empty!");
$('#MsgSucc').attr("style", "background-color:White;");
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
else {
$('#spnVerificationErr').hide();
$('#MsgSucc').attr("style", "background-color:#dfe5e6;");
$('#txtPassword').removeClass('outLineRed');
}
}
);
只需用
blur
eventOk检查问题。我会用blur检查问题。我已经编辑了这个问题,你能研究一下吗..并提供你的建议。如果答案确实是你需要的,你会接受吗?太好了,很高兴它有帮助;)