2

The controls in the ASP.NET

<asp:TextBox ID="txtEnd" runat="server" placeholder="12:59"></asp:TextBox>
<asp:RadioButtonList ID="rblTime2" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
    <asp:ListItem>AM</asp:ListItem>
    <asp:ListItem>PM</asp:ListItem>
</asp:RadioButtonList>

CustomValidator

<asp:CustomValidator ID="ValidateStartTime" ControlToValidate="txtEnd" OnServerValidate="ValidateStartTimeFun" runat="server" ErrorMessage="*required"></asp:CustomValidator>

CodeBehind

protected void ValidateStartTimeFun(object source, ServerValidateEventArgs args)
{
    try
    {   if (txtStart.Text != "" && rblTime.SelectedValue != null )
        { args.IsValid = true; }}
    catch (Exception ex)
        { args.IsValid = false; }
}

It doesn't even give me a *required if I change the entire CodeBehind to this;

protected void ValidateStartTimeFun(object source, ServerValidateEventArgs args)
{
    args.isValid = false;
}
Stachu
  • 5,677
  • 3
  • 30
  • 34
  • Do you have anything in the text box, as the custom validator will only fire on an empty textbox if `ValidateEmptyText='True'` – freefaller Jan 03 '13 at 15:45
  • You are reffering to 2 method names: `ValidateEndTimeFun` and `ValidateStartTimeFun`. Are you sure you are assigning the right method to `OnServerValidate`? – Alex Filipovici Jan 03 '13 at 15:46
  • Did you try it removing the placeholder attribute from the textbox? – erichste Jan 03 '13 at 16:02

5 Answers5

2

If you're validating empty input, custom validators don't fire if you set the ControlToValidate property AND don't set the ValidateEmptyText property to true, otherwise the framework expects you to use a RequiredFieldValidator.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.validateemptytext.aspx

gfyans
  • 1,236
  • 1
  • 16
  • 21
  • Didn't seem to work. I'm using a CustomValidator because I'm validating both at once, otherwise RequiredWould be better. (Don't want two *requireds) to show up. – Stachu Jan 03 '13 at 15:48
  • I wasn't clear enough, I meant the framework expects you to use a RFV if you're validating a textfield (which is essentially what you're telling the CustomValidator to do in the ControlToValidate property), that's why the CustomValidator also has the ValidateEmptyText property. Let me try this and see if I can get it working... – gfyans Jan 03 '13 at 15:55
  • hey, that's working for me. When I set ValidateEmptyText to True, it fires my server side validation method, when I remove that property, it doesn't. – gfyans Jan 03 '13 at 16:04
  • One last thing, you could set your validation logic to this: args.IsValid = (txtEnd.Text != "" && rblTime2.SelectedValue != null); – gfyans Jan 03 '13 at 16:05
0

If you set the ControlToValidate property the CustomValidator won't fire if the user doesn't enter/select anything. But if you omit it it will fire always when the form is submitted.

So remove it.

<asp:CustomValidator ID="ValidateEndTime" 
    OnServerValidate="ValidateEndTimeFun" 
    runat="server" ErrorMessage="*required">
</asp:CustomValidator>

Since you're validating multiple controls i would do it that way, otherwise you could also set the ValidateEmptyText property to true as gfyans has mentioned.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Took it out, but it's still not working. No *required showing up for me even when I change codeBehind to args.isValid = false; – Stachu Jan 03 '13 at 15:58
  • @statue: Where is your submit button? Both the TextBox and the RadioButtonList don't postback immediately when the user selects/enters something. Therefore you have you set `AutoPostBack=true`. – Tim Schmelter Jan 03 '13 at 16:03
0

I think that the ValidationGroup element is missing.

You should the same value for ValidationGroup it to the validator and to the button, too (or checking it programmatically using Validate method of the Page) that has to check the value.

Emanuele Greco
  • 12,551
  • 7
  • 51
  • 70
0

It's been a long time since I used it without an updatepanel as this is also an answer I found by Sarawut Positwinyu:

Custom Validator, when placing in formview will not show its error message after
server-side validation (though it has been validated and result is invalid) the 
mean to fix this in to wrap it by a Update Panel. 

I also remember always putting in the validationgroup="name of the group" for all controls which are validatedand putting validateemptytext to true for the custom validator.

answer reference

Community
  • 1
  • 1
Kevin Hendricks
  • 785
  • 1
  • 8
  • 36
0

why don't you use different validator for different controls? and also to work custom validator in server just discard the ControlToValidate property from the tag. and also check the condition you have used in code behind.

Rashedul.Rubel
  • 3,446
  • 25
  • 36