views:

559

answers:

5

The HTML5 spec defines some very interesting validation components, including pattern (for validating against a Regexp) and required (for marking a field as required). As best I can tell, however, no browser yet actually does any validation based on these attributes.

I found a comparison of HTML5 support across engines, but there is no information about validation there. In the browsers I've tried (Firefox 3.5.8 and Safari 4.0.4), no object has a checkValidity() method, so I can't run the validations even though I can define them.

Is there any support for this feature out there so I can experiment?

+2  A: 

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.

David Thomas
+1  A: 

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().

Adam
+3  A: 

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.

Arne Roomann-Kurrik
You need to call checkValidity on the form, not the inputs.
miketaylr
Where do you see that? http://dev.w3.org/html5/spec/Overview.html says checkValidity is a member function on HTMLObjectElement and: "When the checkValidity() method is invoked, if the element is a candidate for constraint validation... the user agent must fire a simple event named invalid that is cancelable ... at the element and return false. "The same spec defines "candidate for constraint validation" as "A listed form-associated element is a candidate for constraint validation except when a condition has barred the element from constraint validation."Seems that inputs apply.
Arne Roomann-Kurrik
My understanding was that calling `checkValidity()` on the form would simply delegate to each of its `input`s (and `select`s and `textarea`s) in turn, but I can't say I actually have a reference for that.
James A. Rosen
That certainly sounds reasonable, but it should also work on inputs as well
Arne Roomann-Kurrik
+1  A: 

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/

miketaylr
+1  A: 

THANK YOU for having this discussion! I just spent > 8 hours trying to determine why my Javascript function named checkValidity() was being totally ignored by Chrome! I had no idea it was a reserved function in HTML 5. Hopefully anyone else out there tweaking sites for Chrome/HTML 5 will find this thread before wasting as much time as I did!

Bill Carey