Skip to main content

Notifications

Announcements

No record found.

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

  • Community Member Profile Picture
    on at
    RE: RegEx for US and Canada ZipCode validation is causing errors.

    Thank you Goutam!

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: RegEx for US and Canada ZipCode validation is causing errors.

    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);
            }
    
           }
    }



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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans