Regex question

H

Hailstorm

How can I control if "a textbox is empty or not" with regex validation? I
don't want to use required field validator because I have a masked textbox
control and it has "ValidationExpress" property.
For example, if I want to control if it's a time value, I use
"(([0-1][0-9])|([2][0-3]))[:]([0-5][0-9])" but it doesn't work when textbox
is empty. How can I solve this problem? I want user to fill this area.
And by the way, if I try to use a req field validator for this, my masked
textbox doesn't seem on validator's "Control To Validate" property although
it has been inherited from textbox control.


Waiting for your replies.

Sincerely
 
N

news.microsoft.com

I ran into this problem myself -- the validator does not check empty fields
(see below). I ended up inheriting the validator and changing the
client-side javascript to include my own validation function. The downside
is that I had to add that function to a custom WebUIValidation.js (ugh).

One thing you could do is do the validation on the postback, and then
trigger the validator (validator.IsValid = false).


Hope this helps,
Sam Fields

excerpt from WebUIValidation.js ...

function RegularExpressionValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);

#### if length=0 arbitrary "yes" #####
if (ValidatorTrim(value).length == 0)
return true;
var rx = new RegExp(val.validationexpression);
var matches = rx.exec(value);
return (matches != null && value == matches[0]);
}
 
P

Peter Blum

If you would rather get a regex validator that already has the ability to
support blank textboxes instead of writing the code yourself, I can offer a
solution.
I rewrote ASP.NET validation to address all kinds of limitations including
this one. "Professional Validation And More"
(http://www.peterblum.com/vam/home.aspx) includes 22 validators. Its
RegexValidator has a property to allow blank text. All of these validators
support client side validation on many more browsers than the original
validators which are limited to DHTML browsers.

Even if you don't want a third party solution, you may benefit from knowing
the limitations to ASP.NET validation as you plan your site. I put together
a list: http://www.peterblum.com/vam/valmain.aspx.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top