75

Is it possible to check if an input element of an html5 form is valid based on the pattern I set for it? I know the psuedo class stuff.. but i'm hoping something like: document.getElementById('petitionName').valid can return true or false..

I really hope I don't need to create javascript to re-verify this stuff.

5 Answers 5

129

there are two ways to check the validity.

  1. inputElement.checkValidity() returns true or false
  2. inputElement.validity returns the validity-state object. inputElement.validity.valid returns true/false

Instead of using keyup, you can also use the 'input' event. All browser, which have implemented the constraint validation API, have also implemented the input-event.

If you are using option 1 above, Opera has a bug here and will show its validation hint. So you should use 2.

I have created a html5 forms library, which implements all unknown features to incapable browsers + fixes issues in HTML5 browsers. So everything works like defined in the spec, but it's built on top of jQuery. (http://afarkas.github.com/webshim/demos/index.html).

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, I just wanted a checkValidity plugin that was backwards compatible. Seems your library has too many built in functions and the docs are hard to read.
I think the change event is better suited for validity check.
is there a way to use flags (e.g. i) in the HTML pattern?
CheckValidity browser support looks good: developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/…
10

To complement, you can check each element was invalid and, for example, set a focus in the first invalid element, by this code:

var Form = document.getElementById('FormID');
if (Form.checkValidity() == false) {
    var list = Form.querySelectorAll(':invalid');
    for (var item of list) {
        item.focus();
    }
}

1 Comment

This is going to set focus on the last element. I mean, the first then the 2nd, then the Nth, then the last. I guess you're right, it'll set it on the first element -- even keep focus on the first element if the first is also the last. You could use querySelector to avoid all that though.
7

You can use the pattern attribute.

It will validate it client side, so no need to validate it again client side.

Example

jsFiddle.

But, make sure you do it again server side.

Also note, because browser compatibility is quite poor right now, JavaScript is the norm for validating client side.

6 Comments

thanks, I'm trying to avoid using any frameworks, and my pattern attribute is already set. I was hoping I can access the input element via DOM and determine if the element is value or not.
yep, I know :) I have the same setup already made and I want to avoid using any JQuery form validator. I know I can set patterns or types that validate for me. And I've already done that. But I want to call functions once the input element is valid. I'm listening to 'onKeyUp' events on the elements, but no way to verify that they are in fact valid. I have CSS that communicates the element validity but that isn't enough.
@Henry You could loop through the input elements, extracting their pattern attribute and testing the resulting regex.
yeah - thats exactly what I want to avoid :) I did figure it out though: function checkValid(){ document.getElementById('email').validity.valid; }; that works in Chrome and iOS 4.3 :) thanks Alex!
@Henry Ah yeah, I saw that property there and was about to mention it :)
|
1

inputElement.checkValidity() returns boolean, but if you are working on a form-heavy page I would consider using some package like forms.js to handle client-side validation.

You can find more here https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/checkValidity

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
0

Yes, you can check the validity of an input element in an HTML5 form using the checkValidity() method. This method is available on the HTMLInputElement interface and returns a Boolean value indicating whether the input satisfies its validation constraints.

<form>
  <label for="petitionName">Petition Name:</label>
  <input type="text" id="petitionName" pattern="[A-Za-z]+" required>
  <button type="button" onclick="checkValidity()">Check Validity</button>
</form>

<script>
function checkValidity() {
  var petitionNameInput = document.getElementById('petitionName');
  var isValid = petitionNameInput.checkValidity();

  if (isValid) {
    alert('Input is valid!');
  } else {
    alert('Input is not valid!');
  }
}
</script>

You can find more information here:https://webdesign.tutsplus.com/html5-form-validation-with-the-pattern-attribute--cms-25145t

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.