This is a migrated thread and some comments may be shown as answers.

RadMaskedTextBox - Masked Zipcode issue

4 Answers 414 Views
Input
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 15 Feb 2012, 10:09 PM
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>


4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 16 Feb 2012, 06:53 AM
Hello,

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

Thanks,
Princy.
0
Kevin
Top achievements
Rank 1
answered on 16 Feb 2012, 02:30 PM
Thank you, this worked great!
0
Venkata
Top achievements
Rank 1
answered on 11 Mar 2013, 06:54 AM
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
0
Andrey
Telerik team
answered on 14 Mar 2013, 06:34 AM
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.
Tags
Input
Asked by
Kevin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kevin
Top achievements
Rank 1
Venkata
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or