【www.gdgbn.com--正则表达式】

javascript教程 密码验证程序

下面的检查功能,密码字段为空白,只允许字母和数字 - 这一次没有underscopes。因此,我们应该使用一个新的正则表达式来禁止underscopes。这1 / [ W_] /只允许字母和数字。下一步,我们要允许包含字母和数字至少1只密码。为此,我们使用seacrh()方法和两个正则表达式:/(亚利桑那州)+ /和/(0-9)/。

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[W_]/; // allow only letters and numbers
 
    if (fld.value == "") {
        fld.style.background = "Yellow";
        error = "You didn"t enter a password.n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. n";
        fld.style.background = "Yellow";
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.n";
        fld.style.background = "Yellow";
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.n";
        fld.style.background = "Yellow";
    } else {
        fld.style.background = "White";
    }
   return error;
}  

本文来源:http://www.gdgbn.com/aspjiaocheng/23552/