Jquery Field Validation onblur (mvc)

自作多情 提交于 2019-12-26 03:46:25

问题


I want to have the fields in my form be validated after the user clicks out of them. However, I am not familiar with client side validation using Jquery. Can anyone explain how to validate that the field is not empty?

<label id="lblAccountName">Account Name</label>
@Html.TextBoxFor(model => model.Pharmacy.AccountName, new { @id = "txtAccountName", @Name = "txtAccountName" })

回答1:


This can be achieved with JS

var accName = document.getElementById('txtAccountName');
accName.addEventListener('blur', function () {
    if (accName.value != "") {
        //do something
    }
});

Updated Or in jQuery:

var accName = $("#txtAccountName");
accName.blur(function () {
    if (accName.val() != "") {
        //do something
    }
});


来源:https://stackoverflow.com/questions/38379945/jquery-field-validation-onblur-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!