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
.NET ProgrammingASP

Avatar of undefined
Last Comment
Carl Tawn

8/22/2022 - Mon
Carl Tawn

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

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
guvera

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
Kumaraswamy R

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
Gaz124

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?
Carl Tawn

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.
SinghAmandeep

It Should be between -50 and 50 or less than -50 and above 50.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Gaz124

less than -50 and above 50.
ASKER
Gaz124

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;
        }
Carl Tawn

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Kumaraswamy R

<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;
}
ASKER
Gaz124

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;
        }
Carl Tawn

How is your markup defined? Have you put a breakpoint in the validate event to see if it is being hit?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Gaz124

<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
Carl Tawn

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.
ASKER
Gaz124

The brakepoing will not currently be hit, no symbols have been loaded for this document?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Carl Tawn

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".
ASKER
Gaz124

Thats already in there
Carl Tawn

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.