Search code examples
javascriptjqueryunobtrusive-validation

How to give Alert box OR focus on error occur through Unobtrusive Validation JS


I am using the Unobtrusive Jquery for ClientSideValidation,Which is working correctly,

I want to do some enhancement if Possible....!!

  1. How could I show an Alert Box that "something is missing" in a Page?

  2. How could I focus on the error TextBox, Checkbox or any Other control?

Any suggestion would be Helpful...!!


Solution

  • For 2), you can use custom JavaScript code like this:

    $("selector for error messages, e.g. class name")[0].scrollIntoView().focus();
    OR
    document.querySelector("same selector").scrollIntoView().focus();
    

    First method scrolls the page to your element with error, second focuses on that element.