473,241 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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 2402
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Barrow | last post by:
Hi everyone, Does anyone know if it is possible to append your own javascript function onto the end of a button's onclick method when validation controls are used? Here is an example of the...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
1
by: Tim | last post by:
We have a site under development that uses validation web controls to validate the data entered in text boxes. But when the user changes his mind and presses cancel, or navigates using a hyperlink to...
2
by: Barbara Alderton | last post by:
I setup some standard Required Field Validation controls and one Custom validation control on an ASP.NET page (within a user control) to validate text entry. I also setup a Summary Control to post...
2
by: ticars | last post by:
I have a telephone number user control in which I need to validate the data entered (Required field validation and regular expression validation). The phone number is represented as 3 text boxes...
1
by: Rajani | last post by:
Hi, I have designed a form which process database Insert, edit and Delete(datagrid). I input userid,password and privilege in textboxes which has validation controls to verify data keyed in....
2
by: Rajani | last post by:
Hi, I have designed a form which process database Insert, edit and Delete(datagrid). I input userid,password and privilege in textboxes which has validation controls to verify data keyed in....
6
by: Mr. SweatyFinger | last post by:
hi. i have a bunch of controls that get validated with the validation controls in vs2005 there are two that i could not find a control for so i wrote some javascript. so i set my submit...
1
by: Fendi Baba | last post by:
I wrote a simple validation routine. I am trying to trigger validation on group of fields if an option on another radio button is selected. Sub ValidateAdd_ServerValidate(ByVal objSource As...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.