views:

1563

answers:

5

I want to validate string containing only numbers. Easy validation? I added RegularExpressionValidator, with ValidationExpression="/d+".

Looks okay - but nothing validated when only space is entered! Even many spaces are validated okay. I don't need this to be mandatory.

I can trim on server, but cannot regular expression do everything!

+1  A: 

try to use Ajax FilteredTextbox, this will not allow space....... http://www.asp.net/AJAX/AjaxControlToolkit/Samples/FilteredTextBox/FilteredTextBox.aspx

Muhammad Akhtar
why doesn't RegularExpressionValidator work for this?
premium_mesg_dev
this is by design, if you want to disallow space, AJAX FilteredTextbox is best option......
Muhammad Akhtar
+1  A: 

Your question is a little hard to follow, but if you are asking how to write a regular expresion which only accepts digits I think your mistake is in using forward-slash instead of backslash. Try

"\d+"

Scanningcrew
yes, i have tried. let me try explain. entering into text box: "55" - valid. "55x" - fails (this is good). " " - valid! (not so good!)
premium_mesg_dev
Ok, in your example you said you used "/d+" which is very different then "\d+" in regex. Have you tried adding RequiredFieldValidator as well? This might prevent the " " as legal. Not 100% positive, I don't claim to be an asp.net expert ;)
Scanningcrew
A: 

Try using ^\d+$ to force the digits to fill the entire line. ^ = line start $ = line end

Roger Willcocks
IIRC, the ^ and $ are implicit when using a Regex validator.
Cerebrus
I "assumed" that myself, but got bitten by unexpected matches in the past. Oh, wait, that waas pure RegEx. Not sure about the validator.
Roger Willcocks
+4  A: 

This is by design and tends to throw many people off. The RegularExpressionValidator does not make a field mandatory and allows it to be blank and accepts whitespaces. The \d+ format is correct. Even using ^\d+$ will result in the same problem of allowing whitespace. The only way to force this to disallow whitespace is to also include a RequiredFieldValidator to operate on the same control.

This is per the RegularExpressionValidator documentation, which states:

Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control.

A regular expression check of the field in the code-behind would work as expected; this is only an issue with the RegularExpressionValidator. So you could conceivably use a CustomValidator instead and say args.IsValid = Regex.IsMatch(txtInput.Text, @"^\d+$") and if it contained whitespace then it would return false. But if that's the case why not just use the RequiredFieldValidator per the documentation and avoid writing custom code? Also a CustomValidator means a mandatory postback.

Ahmad Mageed
all whitespaces is considered empty? looks like trim on the server is the answer.
premium_mesg_dev
Yes, leaving it blank or adding whitespace is the same. Once a character has been entered the validator will trigger, so entering " 10" is valid. You can keep it as it is, knowing that whatever makes it through will be a valid number or whitespace. In either case trim your value.
Ahmad Mageed
thanks Ahmad. i wish MSDN docs would say "all whitespace is empty"!
premium_mesg_dev
+1  A: 

The RegularExpressionValidator is a nice wrapper for doing regex checks but it will not validate against an empty control. You could use a CustomValidator and then do the regular expression check in a javascript function that you attach to the validator.

It will validate against an empty (blank) control as long as you set the ValidateEmptyText property to true.

You can read more about the CustomValidators on MSDN here.

Kelsey
To add to this, while a clientside script can be used with a CustomValidator it would be wise to also include server side validation to cover all bases.
Ahmad Mageed