web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

RegEx for US and Canada ZipCode validation is causing errors.

(0) ShareShare
ReportReport
Posted on by

Hello guys,
I am trying to validate the zipcode in US and Canada format. I am trying to use the RegEx . I am getting an error that the previous function in the library is not defined at eval(eval at runHandlerInternal).
I am sure the error is becauce of my zipCode validation . But I can't figure out what is the issue:
Here is my code:

function checkZipcode()
{
var zipCode = Xrm.Page.getAttribute("address1_postalcode").getValue();
if(zipCode ! = null)
{
var isValidZip = (^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$);
if(!isValidZip.test(zipCode))
{
alert("zip invalid");
Xrm.Page.getAttribute("address1_postalcode").setValue(null);
}

}
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello Donald ,

    I have found some syntax issue and also space issue. In additon you need to write regex inside / / .

    Here is the working code I have rewrite, please use this. I have tested this is working code.

    function checkZipcode()
    {
    
        var zipCode = Xrm.Page.getAttribute("address1_postalcode").getValue();
        var isValidZip = /(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/; // Added your REGEX inside /YOUR REGEX HERE/
          if(zipCode != null)
            {           
            if(isValidZip.test(zipCode))
            {
                alert("zip valid"); 
            }
            else
            {
                alert("zip invalid");
                Xrm.Page.getAttribute("address1_postalcode").setValue(null);
            }
    
           }
    }



  • Community Member Profile Picture
    on at

    Thank you Goutam!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans