How to put restrictions on entering a password (for example, to enter at least 4 characters), and how to compare a password and a password confirmation?
3 answers
<script> function checkForm() { var p1 = document.getElementByid('pass'); var p2 = document.getElementByid('repass'); if(p1.value.length < 4) //длина меньше 4 { alert('там что-нибудь'); return false; } if(p1.value != p2.value) // пароли не совпали { alert('еще там что-нибудь'); return false; } return true; } </script> <form onsubmit="return checkForm();"> <input type="password" id="pass"><input type="password" id="repass"><input type="submit"> </form>
The simplest example, without cards and women.
|
<form action="/somewhere" method="post" onsubmit="checkFields(this);return false;"> <div id="err"></div> <input type="password" id="p1"> <input type="password" id="p2"> </form> <script> var errCodes = ['Форма успешно отправлена!', 'Пароли не совпадают', 'Пароль не может быть меньше 4-х символов']; function checkFields(obj) { var err = 0; if (document.getElementById('p1').value != document.getElementById('p2').value) { err = 1; } else { if (document.getElementById('p1').value.length <= 4) { err = 2; } } document.getElementById('err').innerHTML = errCodes[err]; } </script>
|
The coolest thing is to use regular expressions. There are a bunch of regular password patterns for password validation.
|