kkkeith 2017-06-13 09:32 采纳率: 100%
浏览 1318
已采纳

js里只有一类关于name的全局变量取不到值,其他的都可以

这是js代码部分,功能是表单验证 只有变量name、inputName取不到值,其他都可以,查了一天了都没答案,求助

var name = document.getElementById("name");
var psw = document.getElementById("psw");
var confirmation = document.getElementById("confirmation");
var email = document.getElementById("email");
var phone = document.getElementById("phone");
var submit = document.getElementById("submit");
var nameHint = document.getElementById("name-hint");
var pswHint = document.getElementById("psw-hint");
var conHint = document.getElementById("con-hint");
var emailHint = document.getElementById("email-hint");
var phoneHint = document.getElementById("phone-hint");
var inputName,inputPsw,inputConfirmation,inputEmail,inputPhone;
function checkNull (text) {
    var name = document.getElementById("name");
    if (text == "") {
        nameHint.innerHTML = "名称不能为空";
        nameHint.style.color = "#de000f";
        name.style.border = "solid 1px #de000f";
    }
    else {
        checkLen(text);
    }
}
function checkLen (text) {
    //\x00-\xff以内的是英文(单字节),以外的是双字节
    //匹配不在\x00-\xff范围内的字符
    var nchar = /[^\x00-\xff]{1,}/g;
    var char = /[\w+\S]{1,}/g;
    //转换成数组
    var str = char.exec(text);
    var nstr = nchar.exec(text); 
    if(str==null){
        str="";
    }
    if(nstr==null){
        nstr="";
    }
    //转换成字符串
    var len = nstr.toString().length*2+str.toString().length;
    if (3<len && len<17) {
        nameHint.innerHTML = "名称格式正确";
        nameHint.style.color = "#56b73c";
        name.style.border = "solid 1px #56b73c";
        inputName = true;
    }
    else {
        nameHint.innerHTML = "长度应为4~16个字符";
        nameHint.style.color = "#de000f";
        name.style.border = "solid 1px #de000f";
    }
}
function checkPsw (text) {
    if (text == "") {
        pswHint.innerHTML = "密码不能为空";
        pswHint.style.color = "#de000f";
        psw.style.border = "solid 1px #de000f";
    }
    else {
        checkPswLen(psw.value);
    }
}
function checkPswLen (text) {
    //\x00-\xff以内的是英文(单字节),以外的是双字节
    //匹配不在\x00-\xff范围内的字符
    var nchar = /[^\x00-\xff]{1,}/g;
    var char = /[\w+\S]{1,}/g;
    //转换成数组
    var str = char.exec(text);
    var nstr = nchar.exec(text); 
    if(str==null){
        str="";
    }
    if(nstr==null){
        nstr="";
    }
    //转换成字符串
    var len = nstr.toString().length*2+str.toString().length;
    if (7<len && len<17) {
        pswHint.innerHTML = "密码可用";
        pswHint.style.color = "#56b73c";
        psw.style.border = "solid 1px #56b73c";
        inputPsw = true;
    }
    else {
        pswHint.innerHTML = "长度应为8~16个字符";
        pswHint.style.color = "#de000f";
        psw.style.border = "solid 1px #de000f";
    }
}
function checkConfirmation (text) {
    if (text == "") {
        conHint.innerHTML = "密码不能为空";
        conHint.style.color = "#de000f";
        confirmation.style.border = "solid 1px #de000f";
    }
    else if (text == psw.value) {
        conHint.innerHTML = "密码输入一致";
        conHint.style.color = "#56b73c";
        confirmation.style.border = "solid 1px #56b73c";
        inputConfirmation = true;
    }
    else {
        conHint.innerHTML = "两次密码不一致";
        conHint.style.color = "#de000f";
        confirmation.style.border = "solid 1px #de000f";
    }
}
function checkEmail (text) {
    var at = text.indexOf("@");
    var dot = text.indexOf(".");
    if (at < 1 || dot - at < 2) {
        emailHint.innerHTML = "邮箱格式错误";
        emailHint.style.color = "#de000f";
        email.style.border = "solid 1px #de000f";
    }
    else {
        emailHint.innerHTML = "邮箱格式正确";
        emailHint.style.color = "#56b73c";
        email.style.border = "solid 1px #56b73c";
        inputEmail = true;
    }
}
function checkPhone (text) {
    if (!/1[34578]\d{9}/.test(text)) {
        phoneHint.innerHTML = "手机格式错误";
        phoneHint.style.color = "#de000f";
        phone.style.border = "solid 1px #de000f";
    }
    else {
        phoneHint.innerHTML = "手机格式正确";
        phoneHint.style.color = "#56b73c";
        phone.style.border = "solid 1px #56b73c";
        inputPhone = true;
    }
}
window.onload = function init () {
    var name = document.getElementById("name");
    name.onfocus = function () {
        nameHint.innerHTML = "必填,长度为4~16个字符";
        nameHint.style.color = "#a9a9a9";
        name.style.border = "solid 1px #a9a9a9";
    };
    name.onblur = function () {
        checkNull(name.value);
    };
    psw.onfocus = function () {
        pswHint.innerHTML = "必填,长度为8~16个字符";
        pswHint.style.color = "#a9a9a9";
        psw.style.border = "solid 1px #a9a9a9";
    };
    psw.onblur = function () {
        checkPsw(psw.value);
    };
    confirmation.onfocus = function () {
        conHint.innerHTML = "再次输入相同密码";
        conHint.style.color = "#a9a9a9";
        confirmation.style.border = "solid 1px #a9a9a9";
    };
    confirmation.onblur = function () {
        checkConfirmation(confirmation.value);
    };
    email.onfocus = function () {
        emailHint.innerHTML = "输入邮箱";
        emailHint.style.color = "#a9a9a9";
        email.style.border = "solid 1px #a9a9a9";
    };
    email.onblur = function () {
        checkEmail(email.value);
    };
    phone.onfocus = function () {
        phoneHint.innerHTML = "输入手机";
        phoneHint.style.color = "#a9a9a9";
        phone.style.border = "solid 1px #a9a9a9";
    };
    phone.onblur = function () {
        checkPhone(phone.value);
    };
    submit.onclick = function () {
        if (inputName&&inputPsw&&inputConfirmation&&inputEmail&&inputEmail) {
            alert("提交成功!");
        }
        else {
            if (!inputName) {
                alert("名称格式错误");
            }
            else if (!inputPsw) {
                alert("密码格式错误")
            }
            else if (!inputConfirmation) {
                alert("密码输入不一致")
            }
            else if (!inputEmail) {
                alert("email格式错误")
            }
            else if (!inputPhone) {
                alert("手机格式错误")
            }
            alert("提交失败!");
        }
    };
};
  • 写回答

4条回答 默认 最新

  • kkkeith 2017-06-13 09:58
    关注

    已解决,因为name是保留字所以获取不到..记住了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档