2010-08-17 77 views
0

我有一個文本框,在JSP頁面中JavaScript的文本框驗證

<td><label for="customer.fcDate" class="desc"><bean:message key="label.customer.fcDate" /></label></td> 
      <td> 
       <table><tbody><tr> 
         <td><input type="Text" name="customer.fcDate" id="customer.fcDate" style="customer.fcDate" class="SingleLineTextField" maxlength="10" size="15" tabindex="1" ></td> 
       <td><a href="javascript:NewCal('customer.fcDate','mmddyyyy')"><img align="middle" src="Images/cal.jpg" width="20" height="20" border="0" alt="Pick a date"></a></td> 

       </tr></tbody></table> 
      </td> 

現在我要檢查使用javascript盒子不能爲空。怎麼做?

你能解釋一下嗎?

 <td> <table> 
<tbody><tr> 
<td> 
<input type="Text" name="customer.fcDate" id="customer.fcDate" style="customer.fcDate" class="SingleLineTextField" maxlength="10" size="15" tabindex="1" onblur="nullcheck()"> 
</td> 
<td><a href="javascript:NewCal('customer.fcDate','mmddyyyy')"> 
<img align="middle" src="Images/cal.jpg" width="20" height="20" border="0" alt="Pick a date"></a></td> </tr></tbody></table> </td> 
這樣

?????

回答

1

你只需在Blur上調用這個函數來檢查tex-tbox是空的還是有價值的事件。

function nullcheck() 
    { 
    if(document.getElementById(fcDate).value=="") 
    { 
     alert("Please enter value."); 
     return false; 
    } 
    } 

onblur =「nullcheck()」 - 將其添加到文本框屬性中。

所有最好的!

+0

你能解釋一下嗎? ​​ \t

\t \t \t \t \t \t​​ \t \t \t \t​​Pick a date \t \t \t \t \t \t \t \t
\t \t \t 這樣 ????? – riyana 2010-08-18 09:32:34

+0

關於上面的「input type =」Text「name =」customer.fcDate「,你需要整合這個功能。 – Rubyist 2010-11-08 10:46:04

2
if (document.getElementById("customer.fcDate").value == "") { 
    // BAD! 
} else { 
    // GOOD! 
} 
1

長在你的驗證功能,你會使用這樣的:

var fcDateInput = document.getElementById('customer.fcDate'); 
if (fcDateInput == null || fcDateInput.value.length < 1) { 
    alert("You should select a valid date"); 
} else { 
    // everything goes right... 
}