-1

I want to use custom form validation for my input fields. I want to display a custom message and make borders red of input fields. The problem is that I am unable to do this in jQuery. For Html, I have found the following solution.

<form:input id="valid-to" required="required" type="text" class="custom-html-validation"
oninvalid="this.setCustomValidity('Enter User Name Here')"
oninput="this.setCustomValidity('')"/>

This oninvalid does work correctly and displays message when user submits the form. How can I use similar functionality in jQuery. I have tried the solution from jQuery validation: change default error message but it did not resolve my problem and I got Cannot read properties of undefined (reading 'messages') in browser console.

2
  • This was the solution in the link which I used stackoverflow.com/questions/2457032/…) Commented Oct 28, 2022 at 11:40
  • Cannot read properties of undefined means that you made a critical mistake with how you constructed your methods, called your methods, or included your scripts. Misspelling a rule or calling a rule that does not exist can cause this error. Since you have not shown us how you came to this error, there is no way anyone could help you solve it. Show us the working example of your code that reproduces the error. Commented Oct 28, 2022 at 17:29

1 Answer 1

-1

After some research, I found the following solution:

$(document).on('focus', '.custom-html-validation', function(){
    $(this).get(0).setCustomValidity('Please enter valid information');
})

The above code shows error message as Please enter valid information when submit button is clicked

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.