如果文本框为空,则按Tab键按错误抛出错误

时间:2014-06-17 07:44:09

标签: javascript jquery asp.net-mvc-3

我有注册屏幕,其中包含用户名,密码,验证码,电子邮件ID等。我需要在jquery中进行验证。验证是如果按Tab而不在用户名文本框中输入任何值,它会向用户抛出错误消息或一些信息,说明用户名不应为空。我是新的jquery,完成了几次验证,但它无法正常工作。有什么建议?这是在MVC cshtml页面中。

MVC cshtml页面

@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("");

在选项卡上按下它需要显示验证消息,如果我按Tab键而不输入任何值,则应显示参考号不应为空

2 个答案:

答案 0 :(得分:1)

$(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("");
            }
        }
});

答案 1 :(得分:0)

我自己已经找到了答案。在这里为其他人提供帮助,

 $('#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');
            }

        }
        );