如何使用javascript检查文本框是否为空
我有一个带有一些文本框的jsp页面。现在我想用一些信息填充它们,然后单击submit按钮。但是我需要检查这个文本框是否为空如何使用javascript检查文本框是否为空,javascript,Javascript,我有一个带有一些文本框的jsp页面。现在我想用一些信息填充它们,然后单击submit按钮。但是我需要检查这个文本框是否为空 如何做到这一点?Canonical不使用针对旧浏览器添加了修剪原型的框架 <html> <head> <script type="text/javascript"> // add trim to older IEs if (!String.trim) { String.prototype.trim = function() {retu
如何做到这一点?Canonical不使用针对旧浏览器添加了修剪原型的框架
<html>
<head>
<script type="text/javascript">
// add trim to older IEs
if (!String.trim) {
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g, "");};
}
window.onload=function() { // onobtrusively adding the submit handler
document.getElementById("form1").onsubmit=function() { // needs an ID
var val = this.textField1.value; // 'this' is the form
if (val==null || val.trim()=="") {
alert('Please enter something');
this.textField1.focus();
return false; // cancel submission
}
return true; // allow submit
}
}
</script>
</head>
<body>
<form id="form1">
<input type="text" name="textField1" value="" /><br/>
<input type="submit" />
</form>
</body>
</html>
<pre><form name="myform" method="post" enctype="multipart/form-data">
<input type="text" id="name" name="name" />
<input type="submit"/>
</form></pre>
<script language="JavaScript" type="text/javascript">
var frmvalidator = new Validator("myform");
frmvalidator.EnableFocusOnError(false);
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("name","req","Plese Enter Name");
</script>
将表单对象传入(此)
使用此JavaScript将对您有很大帮助。代码中给出了一些解释
<script type="text/javascript">
<!--
function Blank_TextField_Validator()
{
// Check whether the value of the element
// text_name from the form named text_form is null
if (!text_form.text_name.value)
{
// If it is display and alert box
alert("Please fill in the text field.");
// Place the cursor on the field for revision
text_form.text_name.focus();
// return false to stop further processing
return (false);
}
// If text_name is not null continue processing
return (true);
}
-->
</script>
<form name="text_form" method="get" action="#"
onsubmit="return Blank_TextField_Validator()">
<input type="text" name="text_name" >
<input type="submit" value="Submit">
</form>
使用regexp:\S将匹配非空白字符:除空格、制表符或新行以外的任何字符。如果字符串中有一个字符不是空格、制表符或新行,那么它就不是空的。因此,您只需要搜索一个字符:\S JavaScript:
function checkvalue() {
var mystring = document.getElementById('myString').value;
if(!mystring.match(/\S/)) {
alert ('Empty value is not allowed');
return false;
} else {
alert("correct input");
return true;
}
}
<form onsubmit="return checkvalue(this)">
<input name="myString" type="text" value='' id="myString">
<input type="submit" value="check value" />
</form>
HTML:
function checkvalue() {
var mystring = document.getElementById('myString').value;
if(!mystring.match(/\S/)) {
alert ('Empty value is not allowed');
return false;
} else {
alert("correct input");
return true;
}
}
<form onsubmit="return checkvalue(this)">
<input name="myString" type="text" value='' id="myString">
<input type="submit" value="check value" />
</form>
您也可以使用jQuery进行检查。。这很简单:
<html>
<head>
<title>jQuery: Check if Textbox is empty</title>
<script type="text/javascript" src="js/jquery_1.7.1_min.js"></script>
</head>
<body>
<form name="form1" method="post" action="">
<label for="city">City:</label>
<input type="text" name="city" id="city">
</form>
<button id="check">Check</button>
<script type="text/javascript">
$('#check').click(function () {
if ($('#city').val() == '') {
alert('Empty!!!');
} else {
alert('Contains: ' + $('#city').val());
}
});
</script>
</body>
</html>
jQuery:检查Textbox是否为空
城市:
检查
$('#检查')。单击(函数(){
if($('#city').val()=''){
警报('Empty!!!');
}否则{
警报('Contains:'+$('#city').val());
}
});
无论您选择何种方法,都不会使您免于在后端执行相同的验证。
var frmvalidator=新验证器(“myform”);
frmvalidator.enableFocusOneError(false);
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation(“名称”、“请求”、“请输入名称”);
注意:在使用上述代码之前,您必须添加
gen\u validatorv31.js
文件。不使用javascript最简单的方法是使用required=“”
@mplungjan:此数据已缓存,请耐心等待。它将在大约15分钟内迎头赶上。1)并非所有浏览器都能理解文本形式-你应该使用文档。文本形式2)将表单传递给函数更安全、更简单3)DUDE是怎么回事?可能是DUDETTE+1阻止重定向到另一个页面并解决了我的问题..干杯..@dreamonic-这里您缩进了需要它的代码,但在我看来还不够。函数和if的内容是左对齐的-不需要在script标记和waste 12下缩进代码spaces@mplungjan那是意见的问题。但是我应该缩进JavaScript函数——就像我上次提交的编辑一样。@Dreamonic我不明白为什么要缩进我的代码,到目前为止,如果不滚动就无法读取它。我按我喜欢的缩进方式缩进,以获得最大的可视性,但仍然缩进。无法抗拒,可以吗?:)好吧,既然我们现在要这么做,我就把这个留在这里:没问题。也许他会读它-太糟糕了,它花费了我10元。即使现在,不是所有的代码都可以不滚动阅读。我真的不认为试图升级可读性和代码澄清有什么错。事实上,你不能阅读的代码是一个只粘贴的修剪。window.onload代码和右边的注释正是我感到恼火的地方——请别管我的代码,如果必须,请缩进需要它的代码。
<input type="text" ID="txtName" Width="165px" required=""/>