9

I have a field inside of a form like this:

<input type="text" minlength="20" name="description" id="description" />

When typed into, the minlength validation works great. But if the input's value is set programmatically the validation won't trigger.

var field = document.querySelector("#description");

// type a couple of character into the field
field.validity.tooShort;
// true

field.value = '';
field.validity.tooShort;
// false

Is there a workaround for this? Or a planned fix? Am I using it wrong?

8
  • What do you mean by programmatically like creating an element description programmatically using JQuery ? Commented Nov 21, 2017 at 16:10
  • I don't know why it's not working but as a workaround you can do it manually, get the attribute and the length of the value and compare them Commented Nov 21, 2017 at 16:32
  • Another workaround would be to use a pattern, something like this: pattern=".{20,}" Commented Nov 21, 2017 at 16:33
  • @BASEERHAIDER I mean like setting the value without typing in the field. field.value = "some text" Commented Nov 21, 2017 at 17:44
  • @EdwardLoveall so you want something like that if you do something like this field.value = "some text" and your field has min length 5 so it should show an error. is that true ? Commented Nov 21, 2017 at 18:11

3 Answers 3

2

Stumbled across this today, you can work around it with pattern validation:

<input type="text" pattern="^.{20,}$" name="description" id="description" />

JS:

var field = document.querySelector("#description");
field.value = 'too short';
field.validity.patternMismatch;
// true

field.value = 'very very very very very very long';
field.validity.patternMismatch;
// false
Sign up to request clarification or add additional context in comments.

Comments

0

One can set the customValidation message, which will be picked up by CSS instantly:

    // Browser ignores minlength check when updated programatically.
    // Calling checkValidaty() does not trigger the check, nor does dispatching
    // an input event on the element. So we have to use customValidity().
    function checkInputValidity(inputEl) {
        const required = inputEl.hasAttribute('required');
        const length = inputEl.value.length;
        const minlength = inputEl.getAttribute('minlength');
        if (required && minlength && length < minlength) {
            inputEl.setCustomValidity(`Please fill out this field, you require at least ${length - minlength} more characters.`);
        } else {
            inputEl.setCustomValidity('');
        }
    }

Comments

-2

You can force the validation process by using

field.checkValidity();

See MSDN about checkValidity()

This page is about select but it's of course valid for every input field.

EDIT : My bad, this is not working, you should do the validation manually.

2 Comments

Not working, this returns true when it should return false, check this fiddle
This can only be used to verify required, and will always return` true` when disabled = true

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.