php javascript验证并发送当前和新密码

时间:2014-10-04 04:40:05

标签: javascript php return submit

我正在写一个“更改密码”表单,但我对javascript不太好,我需要一些帮助:)

这是我目前的职能:

function changeformhash(form, currentpwd, password, conf) {
    // Check each field has a value
    if (currentpwd.value    == ''   ||
        password.value      == ''   ||
        conf.value          == '') {

        alert('¡Debes proporcionar todos los datos solicitados!');
        form.currentpwd.focus();
        return false;
    }

    // Check that the current password is sufficiently long (min 6 chars)
    // The check is duplicated below, but this is included to give more
    // specific guidance to the user
    if (currentpwd.value.length < 6) {

        alert('Tu contraseña actual debe contener, como mínimo, 6 carácteres. Por favor, intentalo de nuevo.');
        currentpwd.value    = "";
        password.value      = "";
        conf.value          = "";
        form.currentpwd.focus();
        return false;
    }

    // At least one number, one lowercase and one uppercase letter
    // At least six characters

    re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/;
    if (!re.test(currentpwd.value)) {

        alert('Tu contraseña actual debe contener, como mínimo, 1 númnero y 1 letra mayúscula y minúscula. Por favor, intentalo de nuevo.');
        currentpwd.value    = "";
        password.value      = "";
        conf.value          = "";
        form.currentpwd.focus();
        return false;
    }

    // Check that the new password does not match with the current password
    // and is sufficiently long (min 6 chars)
    // The check is duplicated below, but this is included to give more
    // specific guidance to the user
    if (password.value == currentpwd.value) {

        alert('¡No puedes usar la misma contraseña!');
        currentpwd.value    = "";
        password.value      = "";
        conf.value          = "";
        form.currentpwd.focus();
        return false;
    }

    if (password.value.length < 6) {

        alert('La contraseña debe contener, como mínimo, 6 carácteres. Por favor, intentalo de nuevo.');
        currentpwd.value    = "";
        password.value      = "";
        conf.value          = "";
        form.currentpwd.focus();
        return false;
    }

    // At least one number, one lowercase and one uppercase letter
    // At least six characters

    var re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/;
    if (!re.test(password.value)) {

        alert('La contraseña debe contener, como mínimo, 1 númnero y 1 letra mayúscula y minúscula. Por favor, intentalo de nuevo.');
        currentpwd.value    = "";
        password.value      = "";
        conf.value          = "";
        form.currentpwd.focus();
        return false;
    }

    // Check password and confirmation are the same
    if (password.value != conf.value) {

        alert('¡Las contraseñas no coinciden!');
        currentpwd.value    = "";
        password.value      = "";
        conf.value          = "";
        form.currentpwd.focus();
        return false;
    }

     // Create a new element input, this will be our hashed current password field.
    var currentp = document.createElement("input");

    // Add the new element to our form.
    form.appendChild(p);
    p.name = "currentp";
    p.type = "hidden";
    p.value = hex_sha512(currentpwd.value);

    // Create a new element input, this will be our hashed password field.
    var p = document.createElement("input");

    // Add the new element to our form.
    form.appendChild(p);
    p.name = "p";
    p.type = "hidden";
    p.value = hex_sha512(password.value);

    // Make sure the plaintext password doesn't get sent.
    currentpwd.value    = "";
    password.value      = "";
    conf.value          = "";

    // Finally submit the form.
    form.submit();
    return true;
}

但首先我想问一个问题,为了用户的安全和舒适:如果没有任何安全问题,如果对新密码的检查失败,我可以保留当前密码字段吗?我知道,从第一手资料,一遍又一遍地写密码因为一些错字......真的很烦人:) :( / p>

除了检查相同的密码外,我还想检查类似的密码,同时查看新密码和当前密码,我该怎么做?

实际上,如果所有检查都通过,则没有任何反应,css悬停样式会冻结:

这是我在firefox控制台中得到的:TypeError:Node.appendChild的参数1不是对象。

我实际上正在使用input type =“button”因为即使javascript返回false也会发送提交,所以...我怎么能使用input type =“submit”并阻止发送if if returns false?

提前致谢!

0 个答案:

没有答案