用于检查字段是否为空的 Javascript 函数

标签 javascript jquery html css

以下函数可用于检查用户是否在给定字段中输入了任何内容。空白字段可以表示两种值。零长度字符串或 NULL 值。

function required()  
{  
var empt = document.forms["form"]["text"].value;  
if (empt == "")  
{  
alert("Please input a Value");  
return false;  
}  
else   
{  
alert('Code has accepted : you can try another');  
return true;   
}  
}  
li {list-style-type: none;  
font-size: 16pt;  
}  
.mail {  
margin: auto;  
padding-top: 10px;  
padding-bottom: 10px;  
width: 400px;  
background : #D8F1F8;  
border: 1px soild silver;  
}  
.mail h2 {  
margin-left: 38px;  
}  
input {  
font-size: 20pt;  
}  
input:focus, textarea:focus{  
background-color: lightyellow;  
}  
input submit {  
font-size: 12pt;  
}  
.rq {  
color: #FF0000;  
font-size: 10pt;  
}  
<!doctype html>  
<html lang="en">  
<head>  
<meta charset="utf-8">  
<title>JavaScript form validation - checking non-empty</title>  
<link rel='stylesheet' href='form-style.css' type='text/css' />  
</head>  
<body>  
<div class="mail">  
<h2>Input your Name and Submit</h2>  
<form name="form1" action="#" onsubmit="required()">  
<ul>  
<li><input type='text' name ='text1'/></li>  
<li class="rq">*Required Field</li>  
<li><input type="submit" name="submit" value="Submit" /></li>  
</ul>  
</form>  
</div>  
<script src="non-empty.js"></script>  
</body>  
</html>  

我正在尝试这段代码,但无法运行脚本。如果有帮助,我也在使用 jquery.js

我该如何解决这个问题?

最佳答案

您的表单和文本字段的名称在 javascript 中是错误的。 我已在以下代码段中更正此问题。

只有行“var empt = document.forms["form1"]["text1"].value;”改变了。

function required()  
{  
var empt = document.forms["form1"]["text1"].value;  
if (empt == "")  
{  
alert("Please input a Value");  
return false;  
}  
else   
{  
alert('Code has accepted : you can try another');  
return true;   
}  
}  
li {list-style-type: none;  
font-size: 16pt;  
}  
.mail {  
margin: auto;  
padding-top: 10px;  
padding-bottom: 10px;  
width: 400px;  
background : #D8F1F8;  
border: 1px soild silver;  
}  
.mail h2 {  
margin-left: 38px;  
}  
input {  
font-size: 20pt;  
}  
input:focus, textarea:focus{  
background-color: lightyellow;  
}  
input submit {  
font-size: 12pt;  
}  
.rq {  
color: #FF0000;  
font-size: 10pt;  
}  
<!doctype html>  
<html lang="en">  
<head>  
<meta charset="utf-8">  
<title>JavaScript form validation - checking non-empty</title>  
<link rel='stylesheet' href='form-style.css' type='text/css' />  
</head>  
<body>  
<div class="mail">  
<h2>Input your Name and Submit</h2>  
<form name="form1" action="#" onsubmit="required()">  
<ul>  
<li><input type='text' name ='text1'/></li>  
<li class="rq">*Required Field</li>  
<li><input type="submit" name="submit" value="Submit" /></li>  
</ul>  
</form>  
</div>  
<script src="non-empty.js"></script>  
</body>  
</html>  

关于用于检查字段是否为空的 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45674752/

相关文章:

javascript - jQuery $.get 不适用于 Phonegap 或 Mobile Safari 中的 "local"文件

Javascript 过滤方法不适用于 if 和 else if

javascript - 通过复选框向 Kendo Grid 添加多个过滤

html - CSS - 列重叠并做奇怪的事情

html - 样式表阻止 table cellpadding 工作

javascript - 仅保存 Javascript 对象的原始属性的最优雅的方法

javascript - Node.js Https POST - 可能的错误代码

jquery - 使用 jQuery UI 添加另一个图像后删除克隆图像

javascript - 当 jQuery .match() 从列表项返回部分匹配时,如何将结果限制为完全匹配?

html - 我的 table 不允许我的复选框并排放置。