[英]Required Field Validator for textbox having onblur value
I have a textbox having onblur value. 我有一个具有onblur值的文本框。 When applying it, its corresponding Required Field Validator is not working.
应用它时,其相应的“必填字段验证器”不起作用。 My code is here
我的代码在这里
<div align="center">
<asp:TextBox ID="tb_age" runat="server"
onfocus="if (this.value == 'Select Age') this.value = '';"
onblur="if (this.value == '') this.value = 'Select Age';"
value="Select Age" >
</asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="req_tb_age"
controltovalidate="tb_age" errormessage="*" SetFocusOnError="True"
Display="Dynamic" />
<asp:Button ID="bt_show" runat="server" text="Show"
CausesValidation="true"
onclick="bt_show_data_Click" OnClientClick="Confirm()"/>
</div>
代替使用onfocus / onblur事件,对TextBox使用placeholder="Select Age"
属性。
As @borissh said you can use placeholder
attribute, if you dont want to use placeholder
you can add attribute InitialValue="Select Age"
to your RequiredFieldValidator
control. 正如@borissh所说,您可以使用
placeholder
属性,如果您不想使用placeholder
,则可以将属性InitialValue="Select Age"
到RequiredFieldValidator
控件中。
generally, the RequiredFieldValidator gets triggered by the onChange event at the client side. 通常, RequiredFieldValidator由客户端的onChange事件触发。 But you are trying to achieve the same by onBlur event.
但是您正在尝试通过onBlur事件实现相同的目的。 Yoy need to add this snippet to trigger the validator for onBlur Instead-
Yoy需要添加此代码段来触发onBlur的验证器,
void Page_Load(object sender, EventArgs e)
{
txtSummary.Attributes.Add("onblur", "ValidatorValidate(" + reqvalSummary.ClientID + ")");
}
Hope this helps. 希望这可以帮助。 Or, go to this link to find out more on this.
或者,转到此链接以了解更多信息。
Instead of writing inline JS you should write your all JS code in one place. 而不是编写内联JS,您应该在一个地方编写所有JS代码。 External file is the best way to manage code though you can write file specific JS on same page.
外部文件是管理代码的最佳方法,尽管您可以在同一页面上编写文件特定的JS。
<asp:TextBox ID="tb_age" runat="server"
onfocus="Validate(this)"
onblur="Validate(this)"
value="" >
</asp:TextBox>
<script type="text/javascript">
function Validate(elem)
{
if (elem.value == '')
{
elem.value = 'Select Age';
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.