Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 show errormessage input field while typing

This is a piece of my current model:

[Required(ErrorMessage = "First name is required!")]
public string FirstName { get; set; }

[Required(ErrorMessage = "Email name is required!")]
[EmailAddress(ErrorMessage = "Invalid email address!")]
public string Email { get; set; }

Is it possible to show the ErrorMessage while typing and not when the "out of focus" of the input occurs? The view is auto-generated.

UPDATE This piece of code was provided by @vendettamit. It is working 50%. The problem is that I have more ErrorMeesages and they all pop out.

$(document).ready(function () {
    var settngs = $('form').data('validator').settings;
    settngs.onkeyup = function () {
        $('form').valid();
    };
    settngs.onfocusout = false;
});
like image 729
DJack Avatar asked Dec 08 '25 06:12

DJack


1 Answers

Get the validator object of unobtrusive jquery validation. Change the event that should trigger the validation.

    $(document).ready(function () {
        var $validatr = $('form').data('validator');
        var settngs = $validatr.settings;

        settngs.onkeyup = function (element, eventType) {
            if (!$validatr.element(element))
            {
                $(this.currentForm).triggerHandler("invalid-form", [this]);
            }
        };

        settngs.onfocusout = false;
    });

Update -

  1. It seems you have to manually trigger the validation for onkeyup. Updated the snippet.

  2. Updated with trigger for single element validation.

Check out all validation options for more customization here.

like image 189
vendettamit Avatar answered Dec 09 '25 20:12

vendettamit