2
<asp:TextBox ID="Fund9" 
             runat="server" 
             columns="4" 
             MaxLength="3" Value="" /> 
<asp:RangeValidator ControlToValidate="Fund9" 
                    MinimumValue="100" 
                    MaximumValue="100" 
                    Type="Integer" 
                    ErrorMessage="Fund 9 must be 0 or 100" 
                    Text="Must be 100% if selected" runat="server" /></td></tr>

As you can see the validation on the above text box only allows the user to enter the number 100. I need it so the user can also enter 0 but no other options. I would appriciate some advice on this.

9
  • 5
    if you can ONLY have 100 or 0, why not have a drop down? Commented Feb 11, 2015 at 14:33
  • Due to design issues being forced on me it has to be a text box. Commented Feb 11, 2015 at 14:34
  • 2
    Ugh. If you're forced into a textbox, look at a RegularExpressionValidator Commented Feb 11, 2015 at 14:35
  • 1
    Then you need to use a CustomValidator. Commented Feb 11, 2015 at 14:36
  • 1
    @MethodMan think outside the textbox : ) Commented Feb 11, 2015 at 14:40

2 Answers 2

6

Use a CustomValidator

<asp:CustomValidator ID="ValidatFund" 
    ControlToValidate="Fund9" ValidateEmptyText="true"
    OnServerValidate="ValidateFund" runat="server"
    ErrorMessage="Fund 9 must be 0 or 100" >
</asp:CustomValidator>


protected void ValidateFund(Object sender, ServerValidateEventArgs e)
{
    e.IsValid = e.Value.Trim() == "0" || e.Value.Trim() == "100";
}

You can additionally provide a ClientValidationFunction in javascript.

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

Comments

3

You can use RegularExpressionValidator. If it is required to enter, you can combine it with RequiredFieldValidator too.

<asp:TextBox ID="Fund9"
    runat="server"
    Columns="4"
    MaxLength="3" Value="" />
<asp:RegularExpressionValidator ID="Fund9RegularExpressionValidator"
    runat="server"
    ValidationExpression="^(0|100)$"
    ErrorMessage="Fund 9 must be 0 or 100" ControlToValidate="Fund9"
    Text="Must be 100% if selected" Display="Dynamic" />
<asp:Button runat="server" ID="SubmitButton" Text="Submit"
    OnClick="SubmitButton_Click" />

<%-- RequiredFieldValidator is optional --%>
<asp:RequiredFieldValidator ControlToValidate="Fund9" Text="Required"
    ErrorMessage="Required is required." runat="Server"
    ID="Fund9RequiredFieldValidator" Display="Dynamic" />

6 Comments

Thanks for the help. Looks like this gives me exactly the functionality I was looking for!
@JD88: a RegularExpressionValidator works also. But note that it doesn't validate empty text (as opposed to the CustomValidator), so you need another RequiredFieldValidator. Have you tried the CustomValidator approach as well? Did it work or what was the problem with it?
I tried the CustomValidator you suggested bellow but i couldn't get it working. Time is an issue for me today so i went with the above option as it worked straight away. It also seems to do the trick for for empty text ( I presume you mean when the text box is left blank) when i add in the RequiredFieldValidator suggested as optional above.
@JD88: maybe you have not added a ClientValidationFunction as mentioned in my answer, so you always have a postback for validation. If you have code that modifies the TextBox in Page_Load(f.e. some databinding stuff) then the ServerValidate event is not triggered.
@Tim Schmelter. That makes sense, I do have other code working on that textbox.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.