locked
Custom regex control not validating empty text RRS feed

  • Question

  • User-331473013 posted

    Hi all

    I've created a custom textbox for which a regular expression property is specified.  The textbox is then validated against the regular expression.

    The control is basically a custom user control, which encapsulates the standard ASP.Net textbox, a regular expression validator, and a textboxwatermarkextender.

    Everything works fine, except when a regular expression is specified which fails for an empty string.  In such a scenario, validation must fail, but instead it passes.  This occurs due to the fact that the textbox's text did not change and therefore validation did not fire for the textbox.

    Any ideas?

    Thursday, August 27, 2009 9:15 AM

Answers

  • User2130758966 posted

    Either:

    Put a RequiredFieldValidator on it as well

    OR

    Post your regex so we can see strengthen it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2009 9:48 AM
  • User-331473013 posted

    OK, I solved the problem.  I eventually decided to revisit the option of adding a required-field validator.  Used my custom user control's RegexValidationExpression property's SET modifier to determine whether a string is required or not (by matching an empty string against the passed-in regex expression), and subsuquently enable or disable the required field validator.


    Quite trivial... amazing how a little bit of sleep deprivation can complicate things :)


    Points to rptHarry for pointing out the most obvious and simplest (best) solution.  Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2009 5:03 AM

All replies

  • User2130758966 posted

    Either:

    Put a RequiredFieldValidator on it as well

    OR

    Post your regex so we can see strengthen it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2009 9:48 AM
  • User-331473013 posted

    Putting an additional required field validator on the control is not really an option, since the regular expression may or may not specify whether an empty string is allowed.


    I can assure you that the regular expression I use is correct.  Here is an example instance of a regular expression used in client code: ^[0-9]{13,13}$


    I have also attempted to "hack" into the page lifecycle in order to force the control to validate.  The control does indeed show a validation error, however, the Page.IsValid attribute evaluates to true (which is a problem, since a wizard control checks this read-only variable to determine whether it should navigate to the next step or cancel navigation).

    protected void txtInput_PreRender(object sender, EventArgs e)
        {
            char[] c = {'^','@'};
            if (_required && txtInput.Text.TrimStart(c).Length == 0 && IsPostBack)
            {
                txtInput.Text = "";
            }
        }

        protected void txtInput_Init(object sender, EventArgs e)
        {
            if (_required && txtInput.Text.Equals("") && IsPostBack)
            {
                txtInput.Text = "^@";
                valRegex.IsValid = false;
            }
        }


        protected void txtInput_TextChanged(object sender, EventArgs e)
        {
            if (_required && txtInput.Text.Trim().Length == 0)
            {
                valRegex.IsValid = false;
            }
        }


    For the time being, if validation for the emty-string condition only occurs on server side it will be acceptable, since the instances of the custom user control is placed inside an updatepanel.  It's been a long day and I wouldn't rule out stupidity as the cause of the problem, however I doubt it.

    Thursday, August 27, 2009 10:05 AM
  • User-331473013 posted

    Hmmm.  Looking at the above code, after a few hours of sleep, I have concluded that it is essentially rubbish.



    Friday, August 28, 2009 2:10 AM
  • User-331473013 posted

    OK, I solved the problem.  I eventually decided to revisit the option of adding a required-field validator.  Used my custom user control's RegexValidationExpression property's SET modifier to determine whether a string is required or not (by matching an empty string against the passed-in regex expression), and subsuquently enable or disable the required field validator.


    Quite trivial... amazing how a little bit of sleep deprivation can complicate things :)


    Points to rptHarry for pointing out the most obvious and simplest (best) solution.  Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2009 5:03 AM