|
-
Sep 16th, 2008, 02:58 PM
#1
Thread Starter
Frenzied Member
[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.
-
Sep 17th, 2008, 05:37 AM
#2
Fanatic Member
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;
}
-
Sep 17th, 2008, 07:40 AM
#3
Thread Starter
Frenzied Member
Re: [2008] RegularExpressionValidator allows spaces
 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.
-
Sep 17th, 2008, 09:04 AM
#4
Thread Starter
Frenzied Member
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.
-
Sep 17th, 2008, 02:03 PM
#5
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)
-
Sep 17th, 2008, 02:07 PM
#6
Thread Starter
Frenzied Member
Re: [2008] RegularExpressionValidator allows spaces
 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
-
Sep 17th, 2008, 02:16 PM
#7
Re: [2008] RegularExpressionValidator allows spaces
Ah, I see, it looked like a small s in this 'custom' font I'm using In any case, my answer still applies - trim it in the codebehind.
-
Sep 17th, 2008, 02:19 PM
#8
Thread Starter
Frenzied Member
Re: [2008] RegularExpressionValidator allows spaces
 Originally Posted by mendhak
Ah, I see, it looked like a small s in this 'custom' font I'm using  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>
Last edited by wey97; Sep 17th, 2008 at 02:26 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|