C# – Asp.Net : Extended range validation

asp.netcvalidation

I'm using Asp.Net 2.0. I have a scenario where i need to check a user input against any of two ranges. For e.g. I need to check a textbox value against ranges 100-200 or 500-600. I know that i can hook up 2 Asp.Net RangeValidators to the TextBox, but that will try to validate the input against both the ranges, an AND condition,if you will. CustomValidator is an option, but how would I pass the 2 ranges values from the server-side. Is it possible to extend the RangeValidator to solve this particular problem?

[Update]
Sorry I didn't mention this, the problem for me is that range can vary. And also the different controls in the page will have different ranges based on some condition. I know i can hold these values in some js variable or hidden input element, but it won't look very elegant.

Best Answer

A CustomValidator should work. I'm not sure what you mean by "pass the 2 ranges values from the server-side". You could validate it on the server-side using a validation method like this:

void ValidateRange(object sender, ServerValidateEventArgs e)
{
    int input;
    bool parseOk = int.TryParse(e.Value, out input);
    e.IsValid = parseOk &&
                ((input >= 100 || input <= 200) ||
                (input >= 500 || input <= 600));
}

You will then need to set the OnServerValidate property of your CustomValidator to "ValidateRange", or whatever you happen to call it.

Is this the sort of thing you're after?

Related Topic