Do any browsers yet support HTML5's checkValidity() method?

后端 未结 6 1433
轮回少年
轮回少年 2020-12-08 09:00

The HTML5 spec defines some very interesting validation components, including pattern (for validating against a Regexp) and required (for marking a field as required

相关标签:
6条回答
  • 2020-12-08 09:05

    If you mean checkValidity() then, yes, Opera supports it.

    Disclosurey thang: I work for Opera.

    0 讨论(0)
  • 2020-12-08 09:07

    Sure. Opera and Chromium. But you can test yourself:

    function supportsValidity(){
      var i = document.createElement('input');
      return typeof i.validity === 'object'
    }
    

    Here's a link to a sandbox where you can see Opera and Chrome in action: http://jsfiddle.net/vaZDn/light/

    0 讨论(0)
  • 2020-12-08 09:11

    checkValidity() is suppost to work on either the form as a whole or an individual input.


    "Additionally, the checkValidity() method can be executed on either an individual field or the form as a whole, and returns true or false. Executing the method will also programmatically fire the invalid event for all invalid fields, or, if executed on a single field, only for that element."

    taken from a List Apart - http://www.alistapart.com/articles/forward-thinking-form-validation/


    "form . checkValidity() Returns true if the form's controls are all valid; otherwise, returns false." http://www.w3.org/TR/2011/WD-html5-20110525/association-of-controls-and-forms.html#constraint-validation

    "valid = element . checkValidity() Returns true if the element's value has no validity problems; false otherwise. Fires an invalid event at the element in the latter case." http://www.w3.org/TR/2011/WD-html5-20110525/forms.html#client-side-form-validation


    W3C - working draft.

    0 讨论(0)
  • 2020-12-08 09:15

    According to Dive into HTML5:

    When an Opera user tries to submit a form with an field, Opera automatically offers RFC-compliant email validation, even if scripting is disabled. Opera also offers validation of web addresses entered into fields, and numbers in fields. The validation of numbers even takes into account the min and max attributes, so Opera will not let you submit the form if you enter a number that is too large.

    (The quoted paragraph is about the last in the article.)

    So far as I'm aware -and bearing in mind I've not tested with Opera 10, I'm taking their word for it- no other browser yet validates forms automatically.

    0 讨论(0)
  • 2020-12-08 09:22

    I tested the following in Google Chrome:

    <!DOCTYPE html>
    <html>
      <body>
        <style>
          .valid { color: #0d0; }
          .invalid { color: #d00; }
        </style>
        <div id="output"></div>
        <script>
          function check(input) {
            var out = document.getElementById('output');
            if (input.validity) {
              if (input.validity.valid === true) {
                out.innerHTML = "<span class='valid'>" + input.id +
                                " is valid</span>";
              } else {
                out.innerHTML = "<span class='invalid'>" + input.id +
                                " is not valid</span>";
              }
            }
            console.log(input.checkValidity());
          };
        </script>
        <form id="testform" onsubmit="return false;">
          <label>Required:
            <input oninput="check(this)" id="required_input" 
                   required />
          </label><br>
          <label>Pattern ([0-9][A-Z]{3}):
            <input oninput="check(this)" id="pattern_input" 
                   pattern="[0-9][A-Z]{3}"/>
          </label><br>
          <label>Min (4):
            <input oninput="check(this)" id="min_input" 
                   type=number min=4 />
          </label><br>
        </form>
      </body>
    </html>
    

    Stangely, the <element>.validity.valid property seems to work correctly, but calling <element>.checkValidity() always returns true, so it looks like that's not implemented yet.

    0 讨论(0)
  • 2020-12-08 09:29

    Opera 10 has some HTML5 form validation http://dev.opera.com/articles/view/improve-your-forms-using-html5/. But, I don't think it has checkValidation().

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