必填字段验证器不起作用
我使用了必填字段验证器,后跟正则表达式验证器,但必填字段验证器不起作用......
<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px"
CssClass="txtStyle" Font-Names="Arial" MaxLength="1000"
ValidationGroup="Valtxt" TabIndex="2" Rows="4">
</asp:TextBox>
<asp:RegularExpressionValidator ID="regValSummary" runat="server"
ControlToValidate="txtSummary" Display="Dynamic"
ValidationExpression="[^<>&#!]*" ValidationGroup="Valtxt">
Invalid characters(<>&#!) are not allowed
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="reqvalSummary" runat="server"
ControlToValidate="txtSummary" ErrorMessage="Summary is required"
ValidationGroup="Valtxt" Display="Dynamic">
</asp:RequiredFieldValidator>
任何人都可以看到问题吗???
I have used a required field validator followed by a regular expression validator but the required field validator is not working.....
<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px"
CssClass="txtStyle" Font-Names="Arial" MaxLength="1000"
ValidationGroup="Valtxt" TabIndex="2" Rows="4">
</asp:TextBox>
<asp:RegularExpressionValidator ID="regValSummary" runat="server"
ControlToValidate="txtSummary" Display="Dynamic"
ValidationExpression="[^<>&#!]*" ValidationGroup="Valtxt">
Invalid characters(<>&#!) are not allowed
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="reqvalSummary" runat="server"
ControlToValidate="txtSummary" ErrorMessage="Summary is required"
ValidationGroup="Valtxt" Display="Dynamic">
</asp:RequiredFieldValidator>
can anyone sees the problem???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
RequiredFieldValidator
由客户端onchange
事件触发。听起来您希望它由onblur
事件触发,这样从文本框移开就会触发验证。在跳转到此之前,我怀疑这就是您所看到的,并验证它是否确实有效,您需要触发
onchange
。为此,请在文本框中输入一些文本,按 Tab 键离开,按 Tab 键返回文本框,清除文本框,然后再次按 Tab 键离开。您现在应该看到RequiredFieldValidator 的错误消息,因为它的内容已更改。回到
onblur
问题。要实现该行为,您可以在代码隐藏中添加onblur
属性,并让它调用ValidatorValidate(...)
JavaScript 方法,如下所示:或者,您可以完成标记中同样的事情。首先,添加此脚本块:
其次,通过添加
onblur="rfvBlur()"
来更新
标记,以便现在看起来像这样:另一种选择是通过将以下属性添加到
标记来验证整个 ValidationGroup(不需要额外的脚本块):The
RequiredFieldValidator
is triggered by the client sideonchange
event. It sounds like you're expecting it to be triggered by theonblur
event, such that tabbing away from the textbox would fire the validation.Before jumping to that, I suspect this is what you are seeing and to validate that it's actually working you need to trigger
onchange
. To do so, enter some text in the textbox, tab away, tab back to it, clear the textbox, then tab away once more. You should now see the RequiredFieldValidator's error message since it's contents have changed.Back to the
onblur
issue. To accomplish that behavior you could add theonblur
attribute in your code-behind and have it call theValidatorValidate(...)
JavaScript method as follows:Alternately, you could accomplish the same thing in markup. First, add this script block:
Second, update the
<asp:TextBox.../>
markup by addingonblur="rfvBlur()"
so that it now looks like this:Yet another option is to validate the entire ValidationGroup by adding the following attribute to your
<asp:TextBox.../>
markup (no additional script block needed):将此行添加到 web.config 的
部分对我有用(当项目升级到 .NET 4.5 时所有验证器停止工作时,我遇到了问题):来源:
http://forums.asp.net/t/1876231.aspx?ASP+Net+4+5+Validation+Controls+not+working+with+AJAX+ToolkitScriptManager1
Adding this line to
<appSettings>
section of web.config worked for me (I had a problem when all validators stopped working when project was upgraded to .NET 4.5):Source:
http://forums.asp.net/t/1876231.aspx?ASP+Net+4+5+Validation+Controls+not+working+with+AJAX+ToolkitScriptManager1
为什么不更改“RegEx”验证器的正则表达式来检查文本框是否为空,而不是使用另一个验证器?
无论如何,您可能没有为引发回发的按钮或控件指定 ValidationGroup="Valtxt"。只需将 ValidationGroup="Valtxt" 添加到将帖子提升到页面的按钮或服务器控件
Why don't you change the regular expression of the "RegEx" validator to check if the textbox is empty instead of use another validator?
Anyway probabily you have not specify ValidationGroup="Valtxt" for the button or the control that raise the postback. Just add ValidationGroup="Valtxt" to the button or the server control that raise the post to the page
我将以下内容放在 btn_Click 事件处理程序的顶部(以防止进一步执行代码),并且在“返回”时,rfv 消息显示...
I put the following at the top of my btn_Click event handler (to prevent further code execution) and upon 'return', the rfv messages show...