HTML5 form required attribute. Set custom validation message?

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

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

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

    The easiest and cleanest way I've found is to use a data attribute to store your custom error. Test the node for validity and handle the error by using some custom html. enter image description here

    le javascript

    if(node.validity.patternMismatch)
            {
                message = node.dataset.patternError;
            }
    

    and some super HTML5

    <input type="text" id="city" name="city" data-pattern-error="Please use only letters for your city." pattern="[A-z ']*" required>
    
    0 讨论(0)
  • 2020-11-22 01:48

    I have made a small library to ease changing and translating the error messages. You can even change the texts by error type which is currently not available using title in Chrome or x-moz-errormessage in Firefox. Go check it out on GitHub, and give feedback.

    It's used like:

    <input type="email" required data-errormessage-value-missing="Please input something">
    

    There's a demo available at jsFiddle.

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

    Here is the code to handle custom error message in HTML5:

    <input type="text" id="username" required placeholder="Enter Name"
      oninvalid="this.setCustomValidity('Enter User Name Here')"
      oninput="this.setCustomValidity('')"/>
    

    This part is important because it hides the error message when the user inputs new data:

    oninput="this.setCustomValidity('')"
    
    0 讨论(0)
  • 2020-11-22 01:51

    The solution for preventing Google Chrome error messages on input each symbol:

    <p>Click the 'Submit' button with empty input field and you will see the custom error message. Then put "-" sign in the same input field.</p>
    <form method="post" action="#">
      <label for="text_number_1">Here you will see browser's error validation message on input:</label><br>
      <input id="test_number_1" type="number" min="0" required="true"
             oninput="this.setCustomValidity('')"
             oninvalid="this.setCustomValidity('This is my custom message.')"/>
      <input type="submit"/>
    </form>
    
    <form method="post" action="#">
      <p></p>
      <label for="text_number_1">Here you will see no error messages on input:</label><br>
      <input id="test_number_2" type="number" min="0" required="true"
             oninput="(function(e){e.setCustomValidity(''); return !e.validity.valid && e.setCustomValidity(' ')})(this)"
             oninvalid="this.setCustomValidity('This is my custom message.')"/>
      <input type="submit"/>
    </form>

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

    It's very simple to control custom messages with the help of the HTML5 oninvalid event

    Here is the code:

    User ID 
    <input id="UserID"  type="text" required 
           oninvalid="this.setCustomValidity('User ID is a must')">
    
    0 讨论(0)
  • 2020-11-22 01:56

    It's very simple to control custom messages with the help of HTML5 event oninvalid

    Here is code:

    <input id="UserID"  type="text" required="required"
           oninvalid="this.setCustomValidity('Witinnovation')"
           onvalid="this.setCustomValidity('')">
    

    This is most important:

    onvalid="this.setCustomValidity('')"
    
    0 讨论(0)
提交回复
热议问题