0

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、名前、アドレス、連絡先など)テキストエリアの空白を確認するにはどうすればよいですか?

よろしくお願いします。

4

1 に答える 1

1

トリム機能を使用して空白を削除します

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();
于 2012-07-18T14:03:37.400 に答える