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?
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.
protected void cvTimeOfDay_ServerValidate(object source, ServerValidateEventArgs args)
{
if(ddlTimeOfDay.SelectedValue == "1" && txtbAddress.Text.Length == 0)
args.IsValid = false;
else
args.IsValid = true;
}
var MyValidation = {
DropdownValidation: function (sender, eventArgs) {
var isValid;
if (eventArgs && $('#ddlTimeOfDay').val() == '1') {
isValid = false;
}
else
isValid = true;
eventArgs.IsValid = isValid; }
}
<asp:DropDownList ID="ddlTimeOfDay" runat="server" ClientIDMode="Static">
<asp:ListItem Text="-Select-" Value="0"></asp:ListItem>
<asp:ListItem Text="PM" Value="1"></asp:ListItem>
<asp:ListItem Text="AM" Value="2"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:TextBox Text="" ID="txtbAddress" runat="server" ClientIDMode="Static"></asp:TextBox>
<asp:CustomValidator ID="cvTimeOfDay" runat="server"
ErrorMessage="MustSelectValue"
ClientValidationFunction="MyValidation.DropdownValidation"
ControlToValidate="txtbAddress" ValidationGroup="group1"
onservervalidate="cvTimeOfDay_ServerValidate" ValidateEmptyText="true"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="group1"/>
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.