RadMaskedTextBox - Masked Zipcode issue

5 posts, 1 answers
  1. Kevin
    Kevin  avatar
    8 posts
    Member since:
    Nov 2010

    Posted 15 Feb 2012 Link to this post

    I have a RadMaskedTextBox used for zip 5 or 9 data.

    I would like to validate always for 5 digit zip code or 9 digit zip code.

    the following regex does not catch zip code 33461-12 which is obviously wrong (it thinks its correct).

    I think there is an issue with the underscores in the mask.  

    Valid zip codes should be:

    33451
    33545-1250

    Anything else should be invalid, ex:

    3314
    33415-1
    33145-12
    33415-123

    Any help would be greatly appreciated.

    <telerik:RadMaskedTextBox ID="txtZipCode" runat="server" Mask="#####-####" SelectionOnFocus="CaretToBeginning"></telerik:RadMaskedTextBox>
        <asp:RegularExpressionValidator
        ID="RegularExpressionValidator1"
        ControlToValidate="txtZipCode"
        ValidationExpression="^((\d{5}(-|\s)\d{0,4})|(\d{5}))$"
        ErrorMessage="Zip code must be numeric nnnnn or nnnnn-nnnn."
        Display="dynamic"
        RunAt="server"></asp:RegularExpressionValidator>


  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Feb 2012 Link to this post

    Hello,

    Try setting the following ValidationExpression.
    ASPX:
    ValidationExpression="(^(\d{5}(-|\s)))(\d{4})?"

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kevin
    Kevin  avatar
    8 posts
    Member since:
    Nov 2010

    Posted 16 Feb 2012 Link to this post

    Thank you, this worked great!
  5. Venkata
    Venkata avatar
    3 posts
    Member since:
    Sep 2012

    Posted 11 Mar 2013 Link to this post

    Hi Princy,

          It is working fine in our app , but when we use same validation in Radgrid it doesn't work, can u give any solution for this.


    <telerik:GridTemplateColumn DataField="ZipCode" HeaderText="ZipCode/PostalCode" SortExpression="ZipCode"
                        ColumnEditorID="GridTextBoxColumnEditor7" HeaderStyle-Width="80px" FilterControlWidth="40px"
                        AutoPostBackOnFilter="true">
                        <ItemTemplate>
                            <%#Eval("ZipCode")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lblZipCode" runat="server" Visible="false" Text='<%# Eval("ZipCode") %>'></asp:Label>
                            <telerik:RadMaskedTextBox ID="txtZipCode" runat="server" Width="150px" Mask="#####-####"
                                Text='<%# Eval("ZipCode") %>' onclick="shouldConfirm = true;">
                            </telerik:RadMaskedTextBox>
                           
                                <asp:RegularExpressionValidator ID="regexpcontactZipCode" runat="server" ControlToValidate="txtZipCode"
                                                            ValidationGroup="contactValidation" Display="Dynamic" ForeColor="Red" ErrorMessage="Should be 5 or 9 Digits"
                                                            ValidationExpression="(^(\d{5}(-|\s)))(\d{4})?"></asp:RegularExpressionValidator>
                           
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>

    Thanks
    M V Prasad
  6. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 14 Mar 2013 Link to this post

    Hi,

    Could you try to set the same validation group to the txtZipCode control or remove the ValidationGroup from the RegularExpressionValidator control. Then check whether the issue still replicates.

    Greetings,
    Andrey
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017