HTML5 form required attribute. Set custom validation message?

前端 未结 14 1542
你的背包
你的背包 2020-11-22 01:26

I\'ve got the following HTML5 form: http://jsfiddle.net/nfgfP/

相关标签:
14条回答
  • 2020-11-22 01:35

    Note: This no longer works in Chrome, not tested in other browsers. See edits below. This answer is being left here for historical reference.

    If you feel that the validation string really should not be set by code, you can set you input element's title attribute to read "This field cannot be left blank". (Works in Chrome 10)

    title="This field should not be left blank."
    

    See http://jsfiddle.net/kaleb/nfgfP/8/

    And in Firefox, you can add this attribute:

    x-moz-errormessage="This field should not be left blank."
    

    Edit

    This seems to have changed since I originally wrote this answer. Now adding a title does not change the validity message, it just adds an addendum to the message. The fiddle above still applies.

    Edit 2

    Chrome now does nothing with the title attribute as of Chrome 51. I am not sure in which version this changed.

    0 讨论(0)
  • 2020-11-22 01:35

    Okay, oninvalid works well but it shows error even if user entered valid data. So I have used below to tackle it, hope it will work for you as well,

    oninvalid="this.setCustomValidity('Your custom message.')" onkeyup="setCustomValidity('')"

    0 讨论(0)
  • 2020-11-22 01:39

    Try this one, its better and tested:

    HTML:

    <form id="myform">
        <input id="email" 
               oninvalid="InvalidMsg(this);" 
               oninput="InvalidMsg(this);"
               name="email"  
               type="email" 
               required="required" />
        <input type="submit" />
    </form>
    

    JAVASCRIPT:

    function InvalidMsg(textbox) {
        if (textbox.value === '') {
            textbox.setCustomValidity('Required email address');
        } else if (textbox.validity.typeMismatch){
            textbox.setCustomValidity('please enter a valid email address');
        } else {
           textbox.setCustomValidity('');
        }
    
        return true;
    }
    

    Demo:

    http://jsfiddle.net/patelriki13/Sqq8e/

    0 讨论(0)
  • 2020-11-22 01:41

    Can be easily handled by just putting 'title' with the field:

    <input type="text" id="username" required title="This field can not be empty"  />
    
    0 讨论(0)
  • 2020-11-22 01:42

    By setting and unsetting the setCustomValidity in the right time, the validation message will work flawlessly.

    <input name="Username" required 
    oninvalid="this.setCustomValidity('Username cannot be empty.')" 
    onchange="this.setCustomValidity('')" type="text" />
    

    I used onchange instead of oninput which is more general and occurs when the value is changed in any condition even through JavaScript.

    0 讨论(0)
  • 2020-11-22 01:46

    Use setCustomValidity:

    document.addEventListener("DOMContentLoaded", function() {
        var elements = document.getElementsByTagName("INPUT");
        for (var i = 0; i < elements.length; i++) {
            elements[i].oninvalid = function(e) {
                e.target.setCustomValidity("");
                if (!e.target.validity.valid) {
                    e.target.setCustomValidity("This field cannot be left blank");
                }
            };
            elements[i].oninput = function(e) {
                e.target.setCustomValidity("");
            };
        }
    })
    

    I changed to vanilla JavaScript from Mootools as suggested by @itpastorn in the comments, but you should be able to work out the Mootools equivalent if necessary.

    Edit

    I've updated the code here as setCustomValidity works slightly differently to what I understood when I originally answered. If setCustomValidity is set to anything other than the empty string it will cause the field to be considered invalid; therefore you must clear it before testing validity, you can't just set it and forget.

    Further edit

    As pointed out in @thomasvdb's comment below, you need to clear the custom validity in some event outside of invalid otherwise there may be an extra pass through the oninvalid handler to clear it.

    0 讨论(0)
提交回复
热议问题