使用 Javascript 验证 asp.net textBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5655739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
validating asp.net textBox using Javascript
提问by Abbas
I have an asp.net TextBox control on my page and a search button whenever user clicks on the search button i want to validate the TextBox for Blank. i want to use a onClientClick event and pass the parameter as my Javascript function will be going to be called from external JS.
我的页面上有一个 asp.net TextBox 控件和一个搜索按钮,每当用户单击搜索按钮时,我想验证文本框是否为空白。我想使用 onClientClick 事件并传递参数,因为我的 Javascript 函数将从外部 JS 调用。
Here's what I tried.
这是我尝试过的。
<asp:TextBox ID="search" runat="server">
</asp:TextBox>
<script language="javascript" type="text/javascript">
function voidsearch(s) {
alert(document.getElementById(s).value);
}
</script>
<asp:ImageButton ID="img1" runat="server" ImageUrl="Dotnetnuke.ico"
OnClientClick="voidsearch('<%= search.ClientID %>'); return false;" />
but this is throwing error. Object Required. i also passed this.search.. but same error. i dont understand why i am getting this error as i have first declared control and then called its ID.
但这是抛出错误。所需对象。我也通过了 this.search.. 但同样的错误。我不明白为什么我会收到这个错误,因为我首先声明了控件,然后调用了它的 ID。
Please anyone help me for this.
请任何人帮助我。
采纳答案by Mikhail
It seems that the “search” TextBox is placed onto INamingContainer, so it is ClientID property cannot be evaluated. Use the approach, described in the infragistics get clientID of dropdown in Rowedit templatethread:
似乎“搜索”文本框放置在 INamingContainer 上,因此无法评估 ClientID 属性。使用infragistics get clientID of dropdown in Rowedit 模板线程中描述的方法:
<asp:TextBox ID="search" runat="server" OnInit="search_Init"></asp:TextBox>
<asp:ImageButton ID="img1" runat="server" ImageUrl="Dotnetnuke.ico"
OnClientClick="voidsearch();" return false;" />
protected void search_Init(object sender, EventArgs e) {
TextBox txt = (TextBox)sender;
string script = string.Format("var _{0} = document.getElementById('{1}');", txt.ID, txt.ClientID);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "ANY_KEY", script, true);
}
<script language="javascript" type="text/javascript">
function voidsearch() {
alert(_search);
alert(_search.value);
}
</script>
回答by Vivek Gauniyal
Validate on blank text box on button click in asp.net
在asp.net中单击按钮时验证空白文本框
<asp:textbox id="name" runat="server"></asp:textbox>
create JavaScript function for validate textbox
为验证文本框创建 JavaScript 函数
<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementByID("<%=name.ClientID%>").value=="")
{
alert("Please enter something");
document.getElementByID("<%=name.ClientID%>").focus();
return false;
}
return true;
}
</script>
call function on button click:
单击按钮时调用函数:
<asp:button id="btn1" runat="server" OnClientClick="return validate()"></asp:button>