Results 1 to 8 of 8

Thread: [2008] RegularExpressionValidator allows spaces

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Angry [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.

  2. #2
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    592

    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;
    }

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    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.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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)

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    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

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    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 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:
    1. <script type="text/javascript">
    2.     function Validate(src, arg) {
    3.         // spaces
    4.         if (arg.Value != "" && arg.Value.trim() == "")
    5.             arg.IsValid = false;
    6.         else
    7.             arg.IsValid = true;
    8.     }
    9. </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
  •  



Click Here to Expand Forum to Full Width