RegularExpressionValidator Doesn't Work WIth MaskedTextBox

2 posts, 0 answers
  1. Holly
    Holly avatar
    11 posts
    Member since:
    Jul 2015

    Posted 23 Jul 2015 Link to this post

    Hi.  I am trying to validate a MaskedTextBox in using a RegularExpressionValidator control.  The validation works on a normal textbox, but I can't get it to work on the MaskedTextBox.  I'm getting the error message Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


    Here is my code:


    <telerik:RadMaskedTextBox ID="txtFederalTaxID" runat="server" Mask="##-#######" CssClass="field" SelectionOnFocus="SelectAll">
                                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator28" runat="server" ErrorMessage="*" ControlToValidate="txtFederalTaxID" InitialValue="" ValidationGroup="Terms"></asp:RequiredFieldValidator>
                                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtFederalTaxID" runat="server" ValidationExpression="^(?!(\d)\1+$|(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){5}\d$|(?:0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(?=4)|6(?=5)|7(?=6)|8(?=7)|9(?=8)){5}\d$)\d{9}$" ErrorMessage="Invalid!" Width="20px"></asp:RegularExpressionValidator>

  2. Konstantin Dikov
    Konstantin Dikov avatar
    2115 posts

    Posted 28 Jul 2015 Link to this post

    Hi Holly,

    I am not sure why you are receiving the error in question, but in a simplified project the code that you have provided does not throw an exception.

    Nevertheless, if you need to validate the RadMaskedTextBox with a RegularExpressionValidator you could try to include the literals as well. In the context of your example, you could try to to include the "-" in the expression. Another option is to use CustomValidator and handle the validation manually.

    On a side note, since the RadMaskedTextBox will validate the expression itself, you can use simple RequieredFieldValidator.

    As for the exception, if the problem persists, please open regular support ticket with sample, runnable project attached, so we can test it locally.

    Konstantin Dikov
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top