Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you require two form fields to match with HTML5?

Tags:

html

forms

Is there a way to require the entries in two form fields to match using HTML5? Or does this still have to be done with javascript? For example, if you have two password fields and want to make sure that a user has entered the same data in each field, are there some attributes, or other coding that can be done, to achieve this?

like image 798
user981178 Avatar asked Feb 04 '12 16:02

user981178


People also ask

Which are two HTML5 attributes that can be used to validate form data?

There are three categories of HTML5 form validation features: HTML attributes on <input> , <select> , and <textarea> elements. (From here on out, I will just refer to them all as <input> cause brevity) CSS pseudo selectors.

Does HTML5 have form validation?

Using built-in form validationOne of the most significant features of HTML5 form controls is the ability to validate most user data without relying on JavaScript. This is done by using validation attributes on form elements.

What is form validation in HTML5?

Form validation is part of browser-side HTML and JavaScript. We can use it to validate form inputs before sending the data to the server. However, we should trust the content that's being sent, so the final validation should still be on the server. With HTML5, form validation is a built-in feature.

Which are the correct input restrictions used for validation purposes in HTML5?

The value must be greater than or equal to the value. There must be a value (if set). Unless the step is set to the any literal, the value must be min + an integral multiple of the step. The number of characters (code points) must not be less than the value of the attribute, if non-empty.


2 Answers

Not exactly with HTML5 validation but a little JavaScript can resolve the issue, follow the example below:

<p>Password:</p> <input name="password" required="required" type="password" id="password" /> <p>Confirm Password:</p> <input name="password_confirm" required="required" type="password" id="password_confirm" oninput="check(this)" /> <script language='javascript' type='text/javascript'>     function check(input) {         if (input.value != document.getElementById('password').value) {             input.setCustomValidity('Password Must be Matching.');         } else {             // input is valid -- reset the error message             input.setCustomValidity('');         }     } </script> <br /><br /> <input type="submit" /> 
like image 72
Faisal Avatar answered Sep 28 '22 04:09

Faisal


You can with regular expressions Input Patterns (check browser compatibility)

<input id="password" name="password" type="password" pattern="^\S{6,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Must have at least 6 characters' : ''); if(this.checkValidity()) form.password_two.pattern = this.value;" placeholder="Password" required>  <input id="password_two" name="password_two" type="password" pattern="^\S{6,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Please enter the same Password as above' : '');" placeholder="Verify Password" required> 
like image 33
Francisco Costa Avatar answered Sep 28 '22 04:09

Francisco Costa