1

I do have a input with the pattern and the title to show the error in case of wrong data, I do need to not use the post method, so I just make some Jquery code to use the input validation, but I can't find how to show the default message of the input

This is the HTML5 input:

<input type="text" id="user" pattern="whatever pattern" title="wrong value" required>

And this is the jquery code:

    $("#inputEnviar").click(
    function(){

        var userValidation = $("#user")[0].checkValidity();

        //validate if the pattern match
        if ( userValidation ){

            //code to do whatever I have to do if the data is valid

        } else {
            //if the data is invalid
            //the input already has a default message to show
            //then, how do I force to show
            $("#user")-> FORCE TO SHOW TO THE DEFAULT ERROR MESSAGE OF THE INPUT
        }

    });

2 Answers 2

1

If the validation fails, in your else code block, set the custom message that you want to notify to the user:

$("#user")[0].setCustomValidity("Please enter at least 5 characters.");

Then, you can use reportValidity() to show that message. From MDN:

The HTMLFormElement.reportValidity() method returns true if the element's child controls satisfy their validation constraints. When false is returned, cancelable invalid events are fired for each invalid child and validation problems are reported to the user.

$("#inputEnviar").click(
  function() {
    var userValidation = $("#user")[0].checkValidity();
    //validate if the pattern match
    if (userValidation) {
      //code to do whatever I have to do if the data is valid
    } else {
      $("#user")[0].setCustomValidity("Please enter at least 5 characters.");
      var isValid = $('#user')[0].reportValidity();
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="user" pattern="whatever pattern" title="wrong value" required>

<input id="inputEnviar" type="button" value="Send">

For old browsers (i.e. IE) you would need to use a polyfill.
There are several implementations around (like this git). This article goes deeper on the topic.

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

2 Comments

It work, I just need to add custom message first using setCustomValidity("")
@Angel - Updated!
0

This should work. The reportValidity() function will show the default message after you have set it with setCustomValidity.

function send() {
    var input = $("#user")[0];
    input.setCustomValidity("");
    if(!input.checkValidity()) {
        input.setCustomValidity("watch me break");
        input.reportValidity();
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="user" pattern="[^,]*" title="Message">
<button onclick="send()">Click</button>

2 Comments

but the message is not yet displayed
I believe I found your solution. You can try the snippet so that you can see it in action.

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.