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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?