我在TextBox上有一個RequiredFieldValidator。當TextBox中沒有任何內容輸入時,這工作正常。現在,我做的另一個驗證是當用戶輸入一些垃圾數據時,我會拋出一個錯誤消息,說「無效輸入」。這是在標籤上。Javascript和必填字段驗證程序
現在情景是在拋出錯誤消息之後,如果用戶清空文本框並單擊按鈕,RequiredFieldValidator可以工作,但標籤上的錯誤消息仍然保持原樣。一旦用戶清空文本框,我想隱藏/刪除它。
爲此,我使用了一個JavaScript函數,但是這個RequiredFieldValidator不起作用。這裏是我的代碼:
<asp:TextBox ID="txtemp" runat="server"></asp:TextBox>
<asp:Button ID="btnstatus" runat="server" ValidationGroup="valgrp1" OnClientClick="Validate()"
CausesValidation="true" onclick="btnstatus_Click"
Text="Fetch status message" BackColor="#ccebff" />
<asp:RequiredFieldValidator ID="Reqfield1" ControlToValidate="txtportalid" ValidationGroup="valgrp1" ErrorMessage="wrong entry" runat="server" />
</div>
<div>
<asp:Label ID="lblerrormsg" runat="server" Font-Bold="true" Visible="false" ForeColor="#FF3300">
</asp:Label>
</div>
的JavaScript:
function Validate()
{
var txt1 = document.getElementById("<%= Txtemp.ClientID %>");
var val1 = txt1.value.replace(/\s/g, "");
if (val1=="")
{
document.getElementById("<%= lblerrormsg.ClientID%>").style.display = 'none';
}
}