基于其他字段的条件验证,c#
我需要创建几个场景:
1)如果下拉列表具有特定值,则将特定文本框设置为必填字段。
2)如果特定文本框有数据,则需要另一个文本框(如果填写了地址字段,则需要城市、州和邮政编码)
我有代码可以从一对看起来正确的 CustomValidators 中调用:
<asp:CustomValidator ID="cvtxt_pat_id" runat="server"
OnServerValidate="txt_pat_idValidate" ControlToValidate="txt_pat_id"
ErrorMessage="Text must be 8 or more characters." Display="Dynamic"/>
protected void txt_pat_idValidate(object sender, ServerValidateEventArgs e)
{
if (ddl_addl_pat_info.SelectedValue.ToString() == "2")
{
e.IsValid = (e.Value.Length > 1);
}
else
{
e.IsValid = true;
}
}
<asp:CustomValidator ID="cvtxt_pat_id" runat="server"
OnServerValidate="addresspartsValidate" ControlToValidate="txt_city"
ErrorMessage="Complete address must be entered." Display="Dynamic"/>
protected void addresspartsValidate(object sender, ServerValidateEventArgs e)
{
if (txt_pat_address.Text.Length > 1)
{
e.IsValid = (e.Value.Length > 1);
}
else
{
e.IsValid = true;
}
}
但据我了解,如果我正在测试的文本框是空的,该框永远不会验证,因此如果它们为空,则不会触发,这使得检查必填字段变得困难。那么……想法?
另外,关于我是否需要同时拥有测试的客户端和服务器版本,我收到了相互矛盾的故事。也许旧版本需要它,但现在不需要了?
I have a couple of scenarios I need to create:
1) if a dropdown has a particular value, make a particular textbox a required field.
2) if a particular textbox has data, make another textbox required (if an address field is filled in, require city, state and zip)
I have code to call from a pair of CustomValidators that looks right:
<asp:CustomValidator ID="cvtxt_pat_id" runat="server"
OnServerValidate="txt_pat_idValidate" ControlToValidate="txt_pat_id"
ErrorMessage="Text must be 8 or more characters." Display="Dynamic"/>
protected void txt_pat_idValidate(object sender, ServerValidateEventArgs e)
{
if (ddl_addl_pat_info.SelectedValue.ToString() == "2")
{
e.IsValid = (e.Value.Length > 1);
}
else
{
e.IsValid = true;
}
}
<asp:CustomValidator ID="cvtxt_pat_id" runat="server"
OnServerValidate="addresspartsValidate" ControlToValidate="txt_city"
ErrorMessage="Complete address must be entered." Display="Dynamic"/>
protected void addresspartsValidate(object sender, ServerValidateEventArgs e)
{
if (txt_pat_address.Text.Length > 1)
{
e.IsValid = (e.Value.Length > 1);
}
else
{
e.IsValid = true;
}
}
But as I understand it, if the textbox I'm testing is empty, the box never validates, so these don't fire if they're blank, which kind of makes it hard to check for a required field. So...thoughts?
Also, I'm getting conflicting stories as to whether or not I need to have BOTH a client and server version of my test. Perhaps it was required in an older version, and now isn't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须稍微向后思考一下。您的自定义验证器应该位于应显示错误的项目上(特定文本框)。文本框上的自定义验证器应检查下拉列表以查看下拉列表是否具有触发文本框所需条件所需的特定条件。如果发现为 true,那么您需要检查文本框是否有输入并相应地返回 args.IsValid。
要使用自定义验证器验证空白文本框,您需要将 ValidateEmptyText 属性设置为“true”。
通常,如果您的网站不能确保打开 JavaScript 来使用该页面,则可以接受两者。有些浏览器可以关闭 JavaScript;如果 JavaScript 被关闭,它就会绕过你的验证。使用客户端验证很好,因为它不会每次都回发来验证输入,而是在客户端上正确执行。
You have to think about it a bit backward. Your custom validator should be on the item that should show the error (the Particular Textbox). The custom validator on the textbox should check the dropdown to see if the dropdown has the particular condition needed to trigger a required condition for the textbox. If it is found to be true then you want to check to see if the textbox has input and return args.IsValid accordingly.
To Validate blank textbox with custom validator you need to set the ValidateEmptyText attribute to "true".
Generally using both is accepted if your site doesn't insure JavaScript is turned on to use the page. Some browsers can have JavaScript turned off; if JavaScript is turned off it bypasses your validation. Using a client side validation is good because it doesn't post back each time to validate input, it does it right on the client.