View Full Version : .NET validation allow numbers and blank/empty

01-07-2009, 12:27 PM
Hi i am tryin g to set a validation on a box but only allow number or leave blank has anyone got any ideas how i can do this ?? here is the code i am using..

<ValidationSettings ErrorDisplayMode="ImageWithTooltip" SetFocusOnError="True" ValidationGroup="AddNewCard">
<RegularExpression ErrorText="Numbers only required, no spaces... example 0308."
ValidationExpression="^([0-9]{4,4})$*" />
<RequiredField ErrorText="Start date required..." IsRequired="True" />


01-07-2009, 05:52 PM
Are you using sometype of 3th party control?

This looks like RadGrid for ASP.NET or someother 3th party control.

01-07-2009, 08:28 PM
Are you really using that code or is it a poorly copied example and you are really using standard ASP.NET server controls (hence Freon22's confusion)?

If so, then your RegularExpressionValidator should look something like this:

<asp:RegularExpressionValidator ID="whatever" runat="server" ControlToValidate="whatever"
ErrorMessage="Numbers only required, no spaces... e.g., 0308" />

This will not do any validation if the user enters nothing in the textbox, so the regular expression does not have to validate no input.

If you really are using some weird and wonderful controls I know nothing about, at least my change to the validation expression may help you.

01-08-2009, 10:36 AM
Hi, i am alerady using the code sampled.. but this bit of code does not allow me to leave the field blank?? i have it so it can handle numbers only but i don't know how let it also allow a blank input box?

so i need they can add 4 numbers or leave the input box blank / empty

hope this gives you more detail...

01-08-2009, 02:22 PM
You need to add this to make the field required:

<asp:RequiredFieldValidator ID="whatever" runat="server"
ControlToValidate="whatever" ErrorMessage="Field is required"></asp:RequiredFieldValidator>

01-10-2009, 12:31 PM
I think the requirement is to accept a 4 digit number or a blank entry. The RequiredFieldValidator control will not allow a blank entry, so it should not be added to the page.

Also, the original code example is using proprietary controls which I have never encountered before. The following validation expression might work:

but as I have never seen these controls (or their documentation) before, then I cannot be sure.

01-10-2009, 05:58 PM
I think the requirement is to accept a 4 digit number or a blank entry.

My apologies. I misread what he was asking.

Here's a way that will work which I personally tested, requiring the validation to be done in the code-behind:

Note: You'll need to add this at the top of your code-behind:

using System.Text.RegularExpressions;

string regexString = @"^[0-9]{4}$";
Regex regex = new Regex(regexString);

if (txtSomething.Text.Length == 0)
lblSomething.Text = "Length is valid!";
if (regex.IsMatch(txtSomething.Text))
lblSomething.Text = "4-digit number!";
lblSomething.Text = "Not a 4-digit number!";

I've used txtSomething as the textbox to validate and lblSomething as a repository for the error message. Not complete code, of course, but just an example of the validation.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum