Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my "oninvalid" attribute let the pattern fail?

Tags:

html

input

This little HTML5 password field works perfectly WITHOUT the oninvalid attribute (the pattern say: minimum 6 characters):

<form>     <input type="password" name="user_password_new" pattern=".{6,}" required />           <input type="submit"  name="register" value="Register" /> </form> 

See the jsFiddle here.

But when I add an oninvalid attribute that gives out a custom error message when user's input does not fit the pattern, the entire field NEVER becomes valid, see the code here:

<form>     <input type="password" name="user_password_new" pattern=".{6,}" oninvalid="setCustomValidity('Minimum length is 6 characters')" required />           <input type="submit"  name="register" value="Register" /> </form> 

See the jsFiddle here.

Can you spot the mistake ?

like image 520
Sliq Avatar asked May 31 '13 23:05

Sliq


People also ask

What is the function of Oninvalid setCustomValidity?

A few basics, reference mdn: oninvalid is triggered when the constraints are not satisfied. setCustomValidity method is used to set the result of the validation; an empty string means the constraint is satisfied, and any other string means there is an error.

How to validate name field in html?

The validation process evaluates whether the input value is in the correct format before submitting it. For example, if we have an input field for an email address, the value must certainly contain a valid email address; it should start with a letter or a number, followed by the @ symbol, then end with a domain name.

How does pattern work in html?

The pattern attribute specifies a regular expression that the <input> element's value is checked against on form submission. Note: The pattern attribute works with the following input types: text, date, search, url, tel, email, and password. Tip: Use the global title attribute to describe the pattern to help the user.

What is Oninvalid?

Definition and Usage The oninvalid event occurs when a submittable <input> element is invalid. For example, the input field is invalid if the required attribute is set and the field is empty (the required attribute specifies that the input field must be filled out before submitting the form).


2 Answers

If you set a value with setCustomValidity() then the field is invalid. That is setting a non-zero length string causes the browser to consider the field invalid. In order to allow for the effects of any other validations you have to clear the custom validity:

<input type="password" name="user_password_new" pattern=".{6,}" required    oninvalid="setCustomValidity('Minimum length is 6 characters')"     oninput="setCustomValidity('')" /> 
like image 171
robertc Avatar answered Oct 02 '22 21:10

robertc


Since I stumbled on the same problem, here is my solution – tested and working with FF, Chrome, IE 10, Edge (Feb 2017).

<form>  <input pattern="1234" oninput="setCustomValidity(''); checkValidity(); setCustomValidity(validity.valid ? '' :'please enter 1234');">  <input type="email" required>  <input type="submit">  </form>

Explanation:

setCustomValidity(''); removes the custom error string which otherwise would always result in an invalid field at the validation process.

checkValidity(); does a manual validation – the same as it is happening at the form submisson. The result is stored in validity.valid.

The second setCustomValidity(validity.valid ? '' :'please enter 1234'); now sets the error string according to the validation result. If the field is valid it needs to be empty, otherwise the custom error string can be set.

like image 37
musicman Avatar answered Oct 02 '22 22:10

musicman