views:

140

answers:

3

I am having problems using a ASP.NET Regular Expression Validator on text boxes. This is a condensed version of my code:

RegularExpressionValidator regex = new RegularExpressionValidator();
regex.ID = "TextBoxRegExValidator" +        ((AVPEditControl)avpControl).ThisFieldRID.ToString(); //random name
regex.ControlToValidate = ((AVPEditControl)avpControl).TextControlID; //this is valid.
regex.ValidationExpression = "\d{3}-\d{2}-\d{4}";
regex.Text = "epic fail";
//later, in an event handler
regex.Display = ValidatorDisplay.None;
regex.ErrorMessage = "";
regex.Validate(); //ERROR
bool valid = AVPEdit.Validator.IsValid;

Where I marked "ERROR" Is where I get a NullReferenceException thrown. I do not see what I am missing here, because I confirmed with a debugger that regex is not null in that context, and neither is the control that it validates.

I wish to have more fine grained control over how the error message is displayed,so thats why I chose not to hook regex into any Panels or such.

Why would I possibly be getting a null reference from that? (Is this a bug in .NET?)

Also, note that this works when I set Visible to 0, but that makes it so IsValid is always true.

+1  A: 

I think you should add it to your controls collection, maybe inside an invisible div control to make it work.

But If you want to validate your textbox programmatically, why don't you just write a method that uses your pattern to validate control and returns validity ?

Canavar
Good point, I probably should.But the whole page is created dynamically. I can't just put it inside a div, the control must be created and added to the page dynamically. I have tried making a Panel(not attached to a page) and adding the validator onto it, but that did nothing.
Earlz
+2  A: 

All the ASP.NET validators must be part of a Page in order to function. You'll note that the Validate method does not return a value; this is because it is not intended to be used the way you're using it.

Inside the Validate method, it is attempting to look up the control by its ID and without a parent naming container, it has no ability to do so. The way you're doing it, it has no value to validate against (because it won't be able to find the control).

I would do one of the following:

1) Put the validator in the ASPX, then use its Validate method and check the IsValid property afterwards. Just set Display to None and it shouldn't show up in your UI.

2) Just run the regex manually. You're writing a lot more code here than would be necessary if you would just use Regex.IsMatch.

(Note that if you use Reflector, browse to RegularExpressionValidator, you'll see where it will attempt to call out to this.NamingContainer among other things that would be null without being part of a control collection)

Adam Sills
I changed it to just use Regex. I didn't realize C# had anything but the bare basics of regex..
Earlz
A: 

I think if you want to use code behind for validation, you can use something like this

Match m = Regex.Match(yourtext,"\d{3}-\d{2}-\d{4}")

if(m.Success) { //valid }

else { //invalid }

Neil