Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set input as invalid

I have two inputs, e.g.

pass:       <input type="password" name="pass" required/> pass again:  <input type="password" name="pass2" required/> 

and I want to compare these inputs, and if they match, set input as valid. I tried this, but I think that prop('valid', true); does not work:

$(document).ready(function() {     $('input[name=pass2]').keyup(function() {         if($('input[name=pass]').val() == $('input[name=pass2]').val()) {             $('#pass_hint').empty();             $('#pass_hint').html('match');             $(this).prop('valid', true);         } else {             $('#pass_hint').empty();             $('#pass_hint').html('mismatch');             $(this).prop('invalid', true);         }     }); }); 

I create a registration form and if passwords are not the same, input field is invalid and I can´t submit this and show me some hint. ...and I don´t know how I set this input as invalid

like image 655
Lubos K. Avatar asked Aug 08 '13 14:08

Lubos K.


People also ask

How do you make an input invalid?

setCustomValidity("Invalid field."); will make the field invalid. field. setCustomValidity(""); will make the field valid unless it fails an HTML5 constraint. Also, if you want the browser's validity message to show up on input or blur you can use field.

Is invalid in JavaScript?

The Invalid JavaScript error is issued in the following situations: JavaScript '<JavaScript node>' has syntax errors. The JavaScript node has one or more syntax errors. Double-click the JavaScript node and correct the lines that have syntax errors.

How do you input validation in HTML?

To validate the form using HTML, we will use HTML <input> required attribute. The <input> required attribute is a Boolean attribute that is used to specify the input element must be filled out before submitting the Form.

What happens in case invalid CSS property is used?

The :invalid selector selects form elements with a value that does not validate according to the element's settings.


1 Answers

In the HTMLInputElement interface, there is no such property as valid or invalid.

You can use the setCustomValidity(error) method with native form validation.

As for your script, here's a demo that should work in all HTML5 compliant browsers:

$('input[name=pass2]').keyup(function () {      'use strict';        if ($('input[name=pass]').val() === $(this).val()) {          $('#pass_hint').html('match');          this.setCustomValidity('');      } else {          $('#pass_hint').html('mismatch');          this.setCustomValidity('Passwords must match');      }  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <form action='#'>      <p>Password:          <input name=pass type=password required>      </p>      <p>Verify:          <input name=pass2 type=password required>      </p>      <p id=pass_hint></p>      <button type=submit>Submit</button>  </form>
like image 144
rink.attendant.6 Avatar answered Sep 21 '22 03:09

rink.attendant.6