11

For some reason html5 validation message is not shown when I'm using an async request.

Here you can see an example.

http://jsfiddle.net/E4mPG/10/

setTimeout(function() {                
  ...
  //this is not working
  target.setCustomValidity('failed!');                
  ...
}, 1000);

When checkbox is not checked, everything works as expected, but when it is checked, the message is not visible.

Can someone explain what should be done?

2
  • 1
    Your goal isn't very clear. I've adjusted your code a bit, but I'm sure you mean something else. Can you specify more detailed what you are checked and what the validation function does? jsfiddle.net/E4mPG/14 Commented Mar 22, 2013 at 12:58
  • When timeout is used, we should see html5 popup message, but it is not shown. Commented Mar 26, 2013 at 12:21

2 Answers 2

5
+100

I figured it out, turns out that the HTML5 validation messages will only popup when a form submit is in progress.

Here is the process behind my solution (when timeout is checked):

  1. Submits the form
  2. Sets the forceValidation flag
  3. Sets the timeout function
  4. When the timeout function is called, resubmit the form
  5. If the forceValidation flag is set, show the validation message

Basically perform two submits, the first one triggered by the button, and the second triggered when the timeout function is called.

jsFiddle

var lbl = $("#lbl");
var target = $("#id")[0];

var forceValidation = false;

$("form").submit(function(){
    return false;
});

$("button").click(function (){                
    var useTimeout = $("#chx").is(":checked");         

    lbl.text("processing...");
    lbl.removeClass("failed");
    target.setCustomValidity('');    
    showValidity();     

    if (forceValidation) {
        forceValidation = false;
        lbl.text("invalid!");
        lbl.addClass("failed");
        target.setCustomValidity('failed!');
        showValidity();
    } else if (useTimeout) {
        setTimeout(function () {
            forceValidation = true;
            $("button").click();
        }, 1000);
    } else {
        lbl.text("invalid without timeout!");
        lbl.addClass("failed");
        target.setCustomValidity('failed!');
        showValidity();            
    }
});

function showValidity() {                    
    $("#lbl2").text(target.checkValidity());
};

I am running on Chrome version 25.0.1364.172 m.

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

Comments

1

HTMLInputElement.reportValidity, which did not exist at the time this question was first answered, also works for this use case.

function showValidity() {                    
    $("#lbl2").text(target.checkValidity());
    target.reportValidity();
};

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.