470,596 Members | 1,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

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

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.ControlToValid ate);
if (isNumberAndNotNull.IsMatch(controlValue))
{
return true;
}
else
{
return false;
}
}

}
}
Nov 18 '05 #1
1 2287
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"
<st**********@ntlworld.com> 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.ControlToVali date);
if (isNumberAndNotNull.IsMatch(controlValue))
{
return true;
}
else
{
return false;
}
}

}
}


Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chris Barrow | last post: by
2 posts views Thread by ticars | last post: by
2 posts views Thread by Rajani | last post: by
6 posts views Thread by Mr. SweatyFinger | last post: by
1 post views Thread by Fendi Baba | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.