Link to home
Start Free TrialLog in
Avatar of Gaz124
Gaz124Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Range Validation in ASP

I have a web form where users must enter a value over 50 and under -50.

Is there a way I can use range validation to NOT allow entry of values within that range?

Thanks
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Don't think you can. You would probably have to use a CustomValidator to do that.
ASKER CERTIFIED SOLUTION
Avatar of Envigo
Envigo
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
Ya, I agree carl tawn's answer.In RangeValidator you can put Minvalue to Maxvalue.
For your scenario you have to use CustomValidator ,

Thanks
Guvera
<asp:RangeValidator ID="RangeValidator1" Runat="server"
  ControlToValidate="TextBox1" Type="Integer"
  ErrorMessage="You must be between -50 and 50"
   MinimumValue="-50" MaximumValue="50"></asp:RangeValidator>

Avatar of Gaz124

ASKER

Envigo and rkworlds that is the opposite of what i'm trying to do. Your ASP will only allow those values and want to NOT allow those values.

Anyone give me more info on how to create a custom validator for this?
Simple example here: http://asp.net-tutorials.com/validation/custom-validator/

All yuo basically do is add one to the form, set its properties as yu would for the other valdiators. Then define a server-side (and optionally client-side) methods to perform the validation. The validation method gets passed the value in the associated control, and you just return true if the value is valid, or false otherwise.
Avatar of SinghAmandeep
SinghAmandeep

It Should be between -50 and 50 or less than -50 and above 50.
Avatar of Gaz124

ASKER

less than -50 and above 50.
Avatar of Gaz124

ASKER

ok I've had a go and getting all sorts of errors. Can anyone help me out? Code Below:

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
        {
            if (e.Value) < 50 Or (e.Value) > -50
                e.IsValid = false;
            else
                e.IsValid = true;
        }
Try:
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
    int i = int.Parse(e.Value);
    if (i < 50 || i > -50)
       e.IsValid = false;
    else
       e.isValid = true;
}

Open in new window

<asp:TextBox runat="server" id="txtNumber" />
<asp:CustomValidator runat="server" id="CustomVal" controltovalidate="txtNumber" onservervalidate="customVal_ServerValidate" errormessage="It Should be between -50 and 50 or less than -50 and above 50. !" />

 CS page


protected void CustomVal_ServerValidate(object sender, ServerValidateEventArgs e)
{
    if(e.Value < -50) or (e.Value > 50)
        e.IsValid = true;
    else
        e.IsValid = false;
}
Avatar of Gaz124

ASKER

All looks good but when i fill in the form no validation is performed, i.e. no error and allows submit even if the value is wrong: Code Below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Templates
{
    public partial class WebForm52 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void validateValue(object sender, ServerValidateEventArgs e)
        {
            int i = int.Parse(e.Value);
            if (i < 50 || i > -50)
                e.IsValid = false;
            else
                e.IsValid = true;
        }
How is your markup defined? Have you put a breakpoint in the validate event to see if it is being hit?
Avatar of Gaz124

ASKER

<asp:CustomValidator ID="CustomValidator1" runat="server"
                    ErrorMessage="Must be Over £50 and under -£50!" OnServerValidate="validateValue" ControlToValidate="totalStoreError"></asp:CustomValidator>

Wouldnt know where to start with a breakpoint
In the code-behind, right-click on a line near the start of your server method and choose Breakpoint > Insert Breakpoint.

When you run your project from the IDE, it should switch to code view when you try and validate the form and stop on the breakpoint. IF it doesn't then your event handler isn't hooked up properly.
Avatar of Gaz124

ASKER

The brakepoing will not currently be hit, no symbols have been loaded for this document?
Do you have your build set to "Debug" or "Release"? The other way to check is to switch to Design View, select your validation control the, in the Properties panel, switch to the Events pane (the one with the lightning bolt) then scroll down and see if there is anything against the ServerValidate event. If there isn't then add "validateValue".
Avatar of Gaz124

ASKER

Thats already in there
In that case you need to use the breakpoint approach, and make sure your configuration is set to Debug. That way you can step through the code as it runs and see precisely what is happening.