检查文本区域中的空白字段

时间:2012-07-18 14:01:54

标签: javascript html

我有3个文本框和1个textarea字段。 ID,姓名,地址,联系方式。 所有都是java脚本,目的是检查空白字段。 我是这样做的:

javascript代码:

function checkForm()    
{
var id=document.getElementById("id").value;
var name=document.getElementById("name").value;
var address=document.getElementById("address").value;
var contact=document.getElementById("contact").value;


if(id.length<1 )        
{
alert("Please enter all the informations!");
return false;
}
if(name.length<1 )      
{
alert("Please enter the name!");
return false;
}
if(address.length<1 )       
{
alert("Please enter the address!");
return false;
}
if(contact.length<1 )       
{
alert("Please enter the contact!");
return false;
}

html代码:

<form method="post" action="clients.php" onSubmit="return checkForm()">
  id <input type="text" name="id" id="id">
  name <input type="text" name="name" id="name">
  address <textarea name="address" id="address"> </textarea>
  contact <input type="text" name="contact" id="contact">
  <input type="submit" name="submit" value="Enter">
</form>
除了textarea之外,所有人都在工作。我正在尝试使用其他一些在互联网上创建的代码,但这些代码无效。维护序列(id然后名称然后地址然后联系....)我如何检查textarea的空白区域?

提前多多感谢。

1 个答案:

答案 0 :(得分:1)

使用trim函数删除空格

var id=document.getElementById("id").value.trim();
var name=document.getElementById("name").value.trim();
var address=document.getElementById("address").value.trim();
var contact=document.getElementById("contact").value.trim();