Custom Validation Controls - how to prevent text box data being used

Discussion in 'ASP .Net' started by Stephen Adam, Nov 13, 2004.

  1. Stephen Adam

    Stephen Adam Guest

    Hi there,

    I have written a custom validation control which checks to see of an input
    field is not empty and contains only numeric data. I was using a regular
    expression validation control but was unable to get it fail if a field was
    blank.

    My problem now is that while my custom validation control will detect if a
    field matches my requirement and will display a error message if it doesnt,
    it wont stop it from being used and sent to my back end code - so I still
    get a nsty crash out if there is an empty field or a field with illegals
    chars in it.

    So my question is how do I get a custom validation control work in the same
    way as a standard validation control and prevent bad input from ever being
    sent to the "code behind".

    Thanks in advance, i've included the revevant code below.


    <%@ Register TagPrefix="CustomValidators" Namespace="CustomValidators"
    Assembly="CustomWebControls" %>

    <asp:TextBox ID="tbRecordToDelete" Runat="server"></asp:TextBox>

    <CustomValidators:NumberValidator
    Runat="server"
    ErrorMessage="Please Enter a Valid ID Number"
    ControlToValidate="tbRecordToDelete"
    Display="static"
    ID="customValidator1"> </CustomValidators:NumberValidator>


    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Text.RegularExpressions;


    namespace CustomValidators
    {


    // Checks control contains only a number and is not null
    public class NumberValidator : System.Web.UI.WebControls.BaseValidator
    {

    public NumberValidator()
    {
    base.EnableClientScript = true;
    }

    protected override bool EvaluateIsValid()
    {

    Regex isNumberAndNotNull = new Regex("^[0-9][0-9]*$");

    string controlValue =
    this.GetControlValidationValue(this.ControlToValidate);
    if (isNumberAndNotNull.IsMatch(controlValue))
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    }
    }
    Stephen Adam, Nov 13, 2004
    #1
    1. Advertising

  2. Stephen Adam

    Scott Allen Guest

    Hi Stephen:

    You might try the ASP.NET stock RangeValidator control. You could set
    the minimum and maximum to Int32.MaxValue and Int32.MinValue if you
    are not concerned with the range, but set the Type property to Integer
    and it will validate all numbers that fit in a 32 bit int.

    For you custom validator, what error message do you get? What does
    GetControlValidationValue look like?

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Sat, 13 Nov 2004 17:00:47 GMT, "Stephen Adam"
    <> wrote:

    >Hi there,
    >
    >I have written a custom validation control which checks to see of an input
    >field is not empty and contains only numeric data. I was using a regular
    >expression validation control but was unable to get it fail if a field was
    >blank.
    >
    >My problem now is that while my custom validation control will detect if a
    >field matches my requirement and will display a error message if it doesnt,
    >it wont stop it from being used and sent to my back end code - so I still
    >get a nsty crash out if there is an empty field or a field with illegals
    >chars in it.
    >
    >So my question is how do I get a custom validation control work in the same
    >way as a standard validation control and prevent bad input from ever being
    >sent to the "code behind".
    >
    >Thanks in advance, i've included the revevant code below.
    >
    >
    ><%@ Register TagPrefix="CustomValidators" Namespace="CustomValidators"
    >Assembly="CustomWebControls" %>
    >
    ><asp:TextBox ID="tbRecordToDelete" Runat="server"></asp:TextBox>
    >
    ><CustomValidators:NumberValidator
    >Runat="server"
    >ErrorMessage="Please Enter a Valid ID Number"
    >ControlToValidate="tbRecordToDelete"
    >Display="static"
    >ID="customValidator1"> </CustomValidators:NumberValidator>
    >
    >
    >using System;
    >using System.Web.UI;
    >using System.Web.UI.WebControls;
    >using System.ComponentModel;
    >using System.Text.RegularExpressions;
    >
    >
    >namespace CustomValidators
    >{
    >
    >
    > // Checks control contains only a number and is not null
    > public class NumberValidator : System.Web.UI.WebControls.BaseValidator
    > {
    >
    > public NumberValidator()
    > {
    > base.EnableClientScript = true;
    > }
    >
    > protected override bool EvaluateIsValid()
    > {
    >
    > Regex isNumberAndNotNull = new Regex("^[0-9][0-9]*$");
    >
    > string controlValue =
    >this.GetControlValidationValue(this.ControlToValidate);
    > if (isNumberAndNotNull.IsMatch(controlValue))
    > {
    > return true;
    > }
    > else
    > {
    > return false;
    > }
    > }
    >
    > }
    >}
    >
    Scott Allen, Nov 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damian
    Replies:
    3
    Views:
    12,486
    Jim Cheshire [MSFT]
    Dec 30, 2003
  2. keithb
    Replies:
    4
    Views:
    733
    Patrick.O.Ige
    Apr 8, 2006
  3. Gnarlodious
    Replies:
    4
    Views:
    17,558
    elizas
    May 5, 2010
  4. Monty
    Replies:
    1
    Views:
    164
    Monty
    Jul 21, 2005
  5. Joel Barsotti

    Custom Web Controls text box drops data?

    Joel Barsotti, Jan 10, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    91
    Victor Garcia Aprea [MVP]
    Jan 10, 2004
Loading...

Share This Page