3

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I think there's a bit of code missing after "EventArgs" in your javascript - I needed to get down to EventArgs.value. This is working: function txtPat_idValidate(sender, eventArgs) { var isValid; if ($('#ddl_addl_pat_info').val() == '2') isValid = !(eventArgs.Value == "" || eventArgs.Value.length < 8) else isValid = true; eventArgs.IsValid = isValid; } Of course, I had to figure out you were using jQuery as well.
Yes, I am using jQuery library and its selector to grab the Textbox by ID. I figured it was obvious because of the syntax and the popularity of jQuery, my fault for not including that. Another note is this method will only work with .NET 4.0. Pre .NET 4.0 you could not set the ClientIDMode and the ID displayed to the Client (ClientID) differed from the ID set in code behind (ID). No code is missing after eventArgs. I’m checking to see if eventArgs is null or has a value. Example:eventArgs = null var eventArgs; if(eventArgs) console.log('Has Value'); else console.log('Is Null'); `
Nice job, this will be handy.
The client-side function needs to check txtbAddress. And if either its local isValid had been named IsValid (i.e. capital "I") or obviated, it would have saved me a good bit of time in following your otherwise good example.
I'll correct it once I re-read it. I haven't seen this code in almost 6 years. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.