tags:

views:

23

answers:

1

Hi Team,

I have a Range Validator as follows. It is for restricting values between 1900 and 2070. However, it fires error when I enter a alphabet also. I want this to be fired only if the user enters integer values. How do I overcome it? Please help..

                        <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtYear"
                            ValidationGroup="report" ErrorMessage="EM1|Year" MaximumValue="2079" MinimumValue="1900"
                            SetFocusOnError="True" Display="Dynamic" Type="Integer">
                                            <img src="../../Images/error.gif" alt="Format" />
                        </asp:RangeValidator>
A: 

You can use custom validator in this case. It may look like this

Use this as your validator code -

  <asp:customvalidator ID="Customvalidator1" ControlToValidate="txtYear" runat="server"
             EnableClientScript="true"
             errormessage="CustomValidator" 
        onservervalidate="Customvalidator1_ServerValidate"></asp:customvalidator>

Use this in your code behind -

protected void Customvalidator1_ServerValidate(object source, ServerValidateEventArgs args)

    {

        int result;

        if (int.TryParse(txtYear.Text,out result))
        {
            if (1900 <= result && result <= 2079)
                args.IsValid = true;
            else
                args.IsValid = false;
        }

        args.IsValid = true;
    }
Manoj
thank you......
Lijo