【问题标题】:Javascript not validating fieldsJavascript不验证字段
【发布时间】:2014-12-15 01:34:16
【问题描述】:

我有以下代码sn-p。 由于某种原因,验证不起作用。

我正在尝试验证两件事:

  1. 没有文本字段为空
  2. 密码字段至少包含 8 个字符

如果其中任何一个是错误的,我应该得到一个错误。但此错误消息从未显示。 下面是sn-p:

function validate() {
    if (document.getElementById("name").value == '') {
        document.getElementById("errorMsg").value=="You have forgotten to enter your name";
    }
    else if (document.getElementById("surname").value == '') {
        document.getElementById("errorMsg").value == "You have forgotten to enter your surname";
    }
    else if (document.getElementById("email").value == '') {
        document.getElementById("errorMsg").value == "You have forgotten to enter your email address";
    }
}
* {
	margin: 0px;
	padding: 0px;
}

#title{
    font: bold 20px Tahoma;
    color: white;
	margin: 10px;
	text-align: left;
    width: 80%;
}

#header{
	padding: 80px;
    font: 14px Tahoma;
}

body{
	width:100%;
	display:-webkit-box;
	-webkit-box-pack: center;
}

#main_container{
	max-width: 1000px;
	margin: 10px 0px;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-box-flex: 1;
}

#control-container{
	display:-webkit-box;
	-webkit-box-orient: horizontal;
}

#label-container{
	-webkit-box-flex: 1;
	margin: 10px;
}

p {
    font: 14px Tahoma;
	margin: 20px;
    width: 350px;
    height: 30px;
}

#element-container{
	width: 500px;
	margin: 20px 0px;
	display: -webkit-box;
	-webkit-box-orient: vertical;
}

#name{
    font: 14px Tahoma;
	margin: 10px;
    width: 350px;
    height: 30px;
}

#surname{
    font: 14px Tahoma;
    margin: 10px;
    width: 350px;
    height: 30px;
}

#email{
    font: 14px Tahoma;
    margin: 10px;
    width: 350px;
    height: 30px;
}

#password{
    font: 14px Tahoma;
    margin: 10px;
    width: 350px;
    height: 30px;
}

input[type="text"]:focus{
    outline: none;
    box-shadow: 0px 0px 5px #7f0000;
    border:1px solid #b20000;
}

input[type="text"]:hover{
    border: 1px solid #999;
}

input[type="text"]:focus:hover{
    outline: none;
    box-shadow: 0px 0px 5px #7f0000;
    border:1px solid #b20000;
}  

input[type="password"]:focus{
    outline: none;
    box-shadow: 0px 0px 5px #7f0000;
    border:1px solid #b20000;
}

input[type="password"]:hover{
    border: 1px solid #999;
    border-radius: 0px;
}

input[type="password"]:focus:hover{
    outline: none;
    box-shadow: 0px 0px 5px #7f0000;
    border:1px solid #b20000;
    border-radius:0;
}  

#register{
    font: 14px Tahoma;
	margin: 20px;
    text-align: center;
}

#btnRegister {
    width: 100px;
    height: 30px;
    margin: 20px;
    text-align: center;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Fastloader - Register for a new account</title>
    <link rel="stylesheet" href="register.css">
	<script src="register.js"></script>
</head>
<body>
    <div id = "main-container">
	<header id = "header">
		<h1>Register for a new account<h1>
	</header>
    <div id= "control-container">
    <section id ="label-container">
        <p><label>Enter your Name:</label></p>
        <p><label>Enter your Surname:</label></p>
        <p><label>Enter your Email address:</label></p>
        <p><label>Choose a strong Password:</label></p>
    </section>
    
    <section id = "element-container">
        <div id = "name">            
            <input name="Name" type="text" id="name"/>
        </div>
        <div id = "surname">
            <input name="Surname" type="text" id="surname"/>
        </div>
        <div id = "email">
            <input name="Email" type="text" id="email" />
        </div>
        <div id = "password">
            <input name="Password" type="password" id = "password"/>
        </div>
    </section>
    </div>
        <footer id = "register">
        <div id = "agreement">
            <label>I have read the Terms and Conditions:</label>
            <input name="agree" type="checkbox" id="agree" />
        </div>
            <div id = "error">
            <p><label id = "errorMsg"></label></p>
            </div>
        <div id = "button">
            <input type = "button" 
                   id="btnRegister" 
                   value = "Register" 
                   onclick="validate();"/>
        </div>
        </footer>
    </div>
</body>
</html>

【问题讨论】:

  • 出错时使用returnfalse,否则在验证函数中使用true
  • 您是说document.getElementById("errorMsg").innerHTML 吗?
  • document.getElementById("errorMsg").value=="You have forgotten to enter your name"; 是二进制表达式,不是赋值语句

标签: javascript html css verification


【解决方案1】:
document.getElementById("errorMsg").value == "You have forgotten to enter your email address";
  1. 您使用== 进行比较,而不是设置新值。在那里你只能使用=

  2. 标签没有value,正确的方式是.innerText

【讨论】:

  • 我认为正确的方法是使用.innerText,因为它是文本,而不是html。 .innerHtml 有效,但您使用的功能不能像那样使用。
  • 仍然无法正常工作。我尝试使用警报,但这仍然没有显示。我正在使用谷歌浏览器
  • document.getElementById("name") 不起作用。我不知道为什么。
  • @AndreasFurster:你使用了 id=name 两次,第一次,&lt;div id=name&gt; 没有 value。 ID 必须是唯一标识符,重命名这些元素之一。
  • 啊,我明白了!没看到,但这不是我的问题... :)
【解决方案2】:

这就是我设法解决的方法:

function validate() {
    if (document.getElementById('name').value.length == 0) {
        alert("You have forgotten to enter your name");
    } else if (document.getElementById("surname").value.length == 0) {
        alert("You have forgotten to enter your surname");
    } else if (document.getElementById("email").value.length == 0) {
        alert("You have forgotten to enter your email address");
    }
}
* {
	margin: 0px;
	padding: 0px;
}

#title{
    font: bold 20px Tahoma;
    color: white;
	margin: 10px;
	text-align: left;
    width: 80%;
}

#header{
	padding: 80px;
    font: 14px Tahoma;
}

body{
	width:100%;
	display:-webkit-box;
	-webkit-box-pack: center;
}

#main_container{
	max-width: 1000px;
	margin: 10px 0px;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-box-flex: 1;
}

#control-container{
	display:-webkit-box;
	-webkit-box-orient: horizontal;
}

#label-container{
	-webkit-box-flex: 1;
	margin: 10px;
}

p {
    font: 14px Tahoma;
	margin: 20px;
    width: 350px;
    height: 30px;
}

#element-container{
	width: 500px;
	margin: 20px 0px;
	display: -webkit-box;
	-webkit-box-orient: vertical;
}

#name{
    font: 14px Tahoma;
	margin: 10px;
    width: 350px;
    height: 30px;
}

#surname{
    font: 14px Tahoma;
    margin: 10px;
    width: 350px;
    height: 30px;
}

#email{
    font: 14px Tahoma;
    margin: 10px;
    width: 350px;
    height: 30px;
}

#password{
    font: 14px Tahoma;
    margin: 10px;
    width: 350px;
    height: 30px;
}

input[type="text"]:focus{
    outline: none;
    box-shadow: 0px 0px 5px #7f0000;
    border:1px solid #b20000;
}

input[type="text"]:hover{
    border: 1px solid #999;
}

input[type="text"]:focus:hover{
    outline: none;
    box-shadow: 0px 0px 5px #7f0000;
    border:1px solid #b20000;
}  

input[type="password"]:focus{
    outline: none;
    box-shadow: 0px 0px 5px #7f0000;
    border:1px solid #b20000;
}

input[type="password"]:hover{
    border: 1px solid #999;
    border-radius: 0px;
}

input[type="password"]:focus:hover{
    outline: none;
    box-shadow: 0px 0px 5px #7f0000;
    border:1px solid #b20000;
    border-radius:0;
}  

#register{
    font: 14px Tahoma;
	margin: 20px;
    text-align: center;
}

#btnRegister {
    width: 100px;
    height: 30px;
    margin: 20px;
    text-align: center;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Fastloader - Register for a new account</title>
    <link rel="stylesheet" href="register.css">
	<script src="register.js"></script>
</head>
<body>
    <div id = "main-container">
	<header id = "header">
		<h1>Register for a new account<h1>
	</header>
    <div id= "control-container">
    <section id ="label-container">
        <p><label>Enter your Name:</label></p>
        <p><label>Enter your Surname:</label></p>
        <p><label>Enter your Email address:</label></p>
        <p><label>Choose a strong Password:</label></p>
    </section>
    
    <section id = "element-container">
        <div id = "name_container">            
            <input name="Name" type="text" id="name"/>
        </div>
        <div id = "surname_container">
            <input name="Surname" type="text" id="surname"/>
        </div>
        <div id = "email_container">
            <input name="Email" type="text" id="email" />
        </div>
        <div id = "password_container">
            <input name="Password" type="password" id = "password"/>
        </div>
    </section>
    </div>
        <footer id = "register">
        <div id = "agreement">
            <label>I have read the Terms and Conditions:</label>
            <input name="agree" type="checkbox" id="agree" />
        </div>
            <div id = "error">
            <p><label id = "errorMsg"></label></p>
            </div>
        <div id = "button">
            <input type = "button" 
                   id="btnRegister" 
                   value = "Register" 
                   onclick="validate();"/>
        </div>
        </footer>
    </div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2020-01-16
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多