views:

983

answers:

4

I have the following ASP page:

<asp:Content ID="Content2" ContentPlaceHolderID="ShellContent" runat="server">
    <form runat="server" id="AddNewNoteForm" method="post"">

        <fieldset id="NoteContainer">
            <legend>Add New Note</legend>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
            <div class="ctrlHolder">
                <asp:Label ID="LabelNoteDate" runat="server" Text="Note Date" 
                    AssociatedControlID="NoteDateTextBox"></asp:Label>
                <asp:TextBox ID="NoteDateTextBox" runat="server" class="textInput" 
                    CausesValidation="True" ></asp:TextBox>
                <asp:CustomValidator 
                        ID="CustomValidator1" 
                        runat="server" 
                        ErrorMessage="CustomValidator" 
                        ControlToValidate="NoteDateTextBox" 
                        OnServerValidate="CustomValidator1_ServerValidate" 
                        Display="Dynamic" 
                        >*</asp:CustomValidator>
            </div>
            <div class="ctrlHolder">
                <asp:Label ID="LabelNoteText" AssociatedControlID="NoteTextTextBox" runat="server" Text="Note"></asp:Label>
                <asp:TextBox ID="NoteTextTextBox" runat="server" Height="102px" 
                    TextMode="MultiLine" class="textInput" ></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ErrorMessage="Note Text is Required" ControlToValidate="NoteTextTextBox">*</asp:RequiredFieldValidator>   

            </div>
            <div class="buttonHolder">
                <asp:Button ID="OkButton" runat="server" Text="Add New Note"  
                    CssClass="primaryAction" onclick="OkButton_Click"/>
                <asp:HyperLink ID="HyperLink1" runat="server">Cancel</asp:HyperLink>
            </div>
        </fieldset>
    </form>
</asp:Content>

and the following code behind for the CustomValidator1_ServerValidate() method:

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {

        if (string.IsNullOrEmpty(args.Value.Trim()))
        {
            args.IsValid = false;
            CustomValidator1.ErrorMessage = "Note Date is Required!";
            return;
        }

        DateTime testDate;
        if (DateTime.TryParse(args.Value, out testDate))
        {
            args.IsValid = true;
            CustomValidator1.ErrorMessage = "Invalid Date!";
        }

    }

It never seems to fail validation no matter what I put in the text box...

Should mention this is ASP.NET 2.0

+4  A: 

In order to use a customvalidator, you also need a requiredfieldvalidator for that same control. Just put a requiredfieldvalidator for NoteDateTextBox and it should fire the customvalidator's server event for you.

Dan Appleyard
A: 

I have a similar issue....

<script language="vb" runat="server">
    Sub AdjDollars_Validate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
        If IsNumeric(e.Value) Then
            e.IsValid = False
        Else
            e.IsValid = False
        End If
    End Sub
</script>
....
<asp:TextBox ID="AdjDollars" runat="server"></asp:TextBox>
...
      <asp:CustomValidator ID="AdjDollarsVal" ControlToValidate="AdjDollars"
        onservervalidate="AdjDollars_Validate" runat="server" 
        ErrorMessage="CustomValidator">

This allow all entries to pass validation

Peter Ker
You should better post it as a new question, more people would look at it and try to help you. (Also it's not an answer to this question.)
sth
A: 

To add to Dan's response, an alternative way of using a CustomValidator is:

  • Remove the ControlToValidate property
  • In your OnServerValidate method, reference the control you are validating instead of using ServerValidateEventArgs.Value, e.g.

Example

Code infront

<asp:ValidationSummary runat="server" DisplayMode="BulletList" ValidationGroup="form" />

<asp:TextBox runat="server" ID="_textbox"/>
<asp:CustomValidator runat="server" 
        ErrorMessage="Please enter the secret" 
        OnServerValidate="TextBoxValidate"
        ValidationGroup="form" 
        Display="None"
        EnableClientScript="false" />
<asp:button runat="server" OnClick="ButtonClick" Text="Press" />

Code behind

protected void ButtonClick(object sender, EventArgs e)
{
    Page.Validate();

    if (Page.IsValid)
    {
        // Do something
    }
}

protected void TextBoxValidate(object sender, ServerValidateEventArgs args)
{
    args.IsValid = _textbox.Text == "secret";
}
Chris S
The form will no load if there is a validation control that does not have its controltovalidate property set. You will receive an exception.
Dan Appleyard
@dan it's not necessary for a custom validator. I've updated my answer to show this.
Chris S
+1  A: 

When you are testing if the textbox is empty, use this ValidateEmptyText="true" on the CustomValidator.

Otherwise required field validation won't work.

Campinho