问题
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