How to show setCustomValidity message/tooltip with

2019-01-08 13:58发布

I'm using forms basic validation to check if email is correct format, then the data is sent by Ajax where it checks if email address is already in use and did the user select country/state or left default values in select boxes.

But for HTML5 form validation to be done submit event is needed, upon clicking submit if it passes that basic form validation Ajax operation is performed, but then when results come in I would like to use the same browser tooltips for interface consistency (and well I like how they look).

So is there a way to make them show up, I was unable to find if there is some special event for them or something like firing submit event but stopping it right away. Right now the field only gets a red edge and error message appears on hovering mouse over it, while the tooltip shows on again clicking submit button.

Also for browsers that don't have native tooltips(in my case Safari) I'm using Webshims Lib and it acts exactly the same as in Chrome and Firefox.

8条回答
劫难
2楼-- · 2019-01-08 14:29

As most people say you have to use input.setCustomValidity("message").

The problem here is that you can't check that validation within the submit event, since you need to do an ajax call and then set setCustomValidity asynchronously.

So basically you have to use the change event of the input field and make the ajax call on every change. Or remove the submit button and use the click event of a normal button, check the unique email in the ajax call and then call submit through javascript.

See an example of the last option using jQuery:

<form action="/sign-in" id="form">
    <input type="email" id="email" required/>
    <button id="submit">Submit</button>
</form>
// we capture the click instead the submit
$("#submit").on("click",function(){
    var $elem = $("#email");
    var email = $elem.val();

    //the ajax call returns true if the email exists
    $.get( "ajax/checkUniqueEmail", function(data) {
        if(data === "true"){
            $elem.setCustomValidity("This email already exists.");
        }else{
            $elem.setCustomValidity("")
        }
        //then we submit the form
        $("#form").submit();
    });
});
查看更多
可以哭但决不认输i
3楼-- · 2019-01-08 14:39

Checkout this link ( Provide custom validation messages using setCustomValidity in HTML 5 pages ).

In the above link, he used "input" type is number and oninput he called the validation function. Let me know is this what you are looking for.

<input type="number" required="true" value="50" min="18" max="60" step="1" oninput="validate(this)">
查看更多
孤傲高冷的网名
4楼-- · 2019-01-08 14:40

Here's a nice live example that custom validates/gives feedback on a form input.

The basic javascript looks like:

function checkPasscode() {
    var passcode_input = document.querySelector("#passcode");

    if (passcode_input.value != "Ivy") {
        passcode_input.setCustomValidity("Wrong. It's 'Ivy'.");
    } else {
        passcode_input.setCustomValidity(""); // be sure to leave this empty!
        alert("Correct!");
    }
}

HTML:

<form>
    <label for="passcode">Enter Passcode:</label>
    <input id="passcode" 
           type="password" 
           placeholder="Your passcode" 
           oninput="checkPasscode();"
           required/>
    <button type="submit">Submit</button>
</form>
查看更多
太酷不给撩
5楼-- · 2019-01-08 14:41

You can find an answer at this link: How to force a html5 form validation without submitting it via jQuery

Basically, you find a submit button and call click on it:

// force form validation
document.getElementById("submitbutton").click()

You can also change validation message after checking if email address is in use or not and then force form validation as above:

document.getElementById("email").setCustomValidity("This email is already registered!");
document.getElementById("submitbutton").click()
查看更多
时光不老,我们不散
6楼-- · 2019-01-08 14:45

You can use setCustomValidity with for the element if the condition is false ( you can do it inside any event handler such as keypress or click ). If the input is NOT valid, SUBMIT. This will trigger the browser's message tooltip.

查看更多
【Aperson】
7楼-- · 2019-01-08 14:48

I thought .checkValidity() would do the trick, but it doesn't trigger the UI.

It sounds like .reportValidity() is going to do what you want. http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#dom-cva-reportvalidity

I don't know if any browsers implement it yet though :/

查看更多
登录 后发表回答