473,396 Members | 1,678 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,396 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 2408
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.