[2008] RegularExpressionValidator allows spaces
I understand anything but RequiredFieldValidator will allow nulls BUT if I specify I don't want to allow spaces why does RegularExpressionValidator allow them?!
The expression I'm using is ^\S+$ but it still allows:
' ', ' ', any number of spaces!
This is not a required field.
Re: [2008] RegularExpressionValidator allows spaces
If it's not required field then why you use the RequiredFieldValidator?
By default, RequiredFieldValidator allows you to enter spaces but it always throws an exception/error after validating. Maybe you want to use CustomValidator for the purpose? In addition if you want to dissalow the users of entering spaces entirely you can use javascript to find if space bar was pressed!
if (window.event.keyCode == 32) {
//Space
return false;
}
Re: [2008] RegularExpressionValidator allows spaces
Quote:
Originally Posted by selanec
If it's not required field then why you use the RequiredFieldValidator?
By default, RequiredFieldValidator allows you to enter spaces but it always throws an exception/error after validating. Maybe you want to use CustomValidator for the purpose? In addition if you want to dissalow the users of entering spaces entirely you can use javascript to find if space bar was pressed!
if (window.event.keyCode == 32) {
//Space
return false;
}
I said I was using the RegularExpressionValidator not the RequiredFieldValidator. I need to know if there is a way to override the behavior of RegularExpressionValidator so validation fails on empty spaces. I'm guessing no.
So everywhere I have a RegularExpressionValidator on a non-required field the I have to manually check if spaces are entered:
Code:
if(string.IsNullOrEmpty(TextBox.Text.Trim())) {
}
so I won't save spaces in my database.
The javascript hack you wrote to check for spaces isn't an option.
Re: [2008] RegularExpressionValidator allows spaces
I can override the EvaluateIsValid() function to return false if the TextBox contains only spaces:
Code:
public class PostalCodeValidator : RegularExpressionValidator {
public PostalCodeValidator(){
this.ValidationExpression = @"^\d{5}(-\d{4})?$";
}
protected override bool EvaluateIsValid() {
Control c = Page.FindControl(this.ControlToValidate);
if (c != null) {
if (c.GetType() == typeof(TextBox)) {
TextBox t = (TextBox)c;
// spaces?
if (t.Text != string.Empty && t.Text.Trim() == string.Empty) {
return false;
}
}
}
return base.EvaluateIsValid();
}
}
and on a submit I can check for if(!Page.IsValid){}
but the Javascript doesn't prevent a submit if the validation doesn't succeed obviously because the script hasn't been modified and I don't think there is a way.
Maybe CustomValidator is my only option.
Re: [2008] RegularExpressionValidator allows spaces
^\S+$
Your regex is wrong then. That means one or more whitespaces! You should allow whitespaces in your input fields but trim it in the codebehind. (Since it's not required, you can be lenient)
Re: [2008] RegularExpressionValidator allows spaces
Quote:
Originally Posted by mendhak
^\S+$
Your regex is wrong then. That means one or more whitespaces! You should allow whitespaces in your input fields but trim it in the codebehind. (Since it's not required, you can be lenient)
^ match beginning of string
\S (capital S) any NON whitespace character ( \s means any whitespace)
+ one or more
$ end of string
Re: [2008] RegularExpressionValidator allows spaces
Ah, I see, it looked like a small s in this 'custom' font I'm using :D In any case, my answer still applies - trim it in the codebehind.
Re: [2008] RegularExpressionValidator allows spaces
Quote:
Originally Posted by mendhak
Ah, I see, it looked like a small s in this 'custom' font I'm using :D In any case, my answer still applies - trim it in the codebehind.
But I want the javascript to prevent a submit.
Picky, I know.
I think it's best to wrap a RegularExpressionValidator and CustomValidator into one control. Should I inherit from one or the other and make the other a private member or inherit from BaseValidator and make both RegularExpressionValidator and CustomValidator private members? Then what? Override Render and render both controls?
CustomValidator has a property named ValidateEmptyText="true" which works perfectly with this client script:
javascript Code:
<script type="text/javascript">
function Validate(src, arg) {
// spaces
if (arg.Value != "" && arg.Value.trim() == "")
arg.IsValid = false;
else
arg.IsValid = true;
}
</script>