How to set restrictions on password entry and password verification

How to set restrictions on entering a password (so that, for example, you enter at least 4 characters), and how to compare the password and password confirmation?

Author: Nicolas Chabanovsky, 2011-05-16

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.

 1
Author: ling, 2011-05-16 11:49:13
<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>
 1
Author: Alex Silaev, 2011-05-16 11:50:32

The coolest thing is to use regular expressions. There are a bunch of templates for password validation rules

 0
Author: Pavel S. Zaitsau, 2011-05-16 20:51:25