Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

RegEx for US and Canada ZipCode validation is causing errors.

Posted on by Microsoft Employee

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
    Community Member Microsoft Employee on at
    RE: RegEx for US and Canada ZipCode validation is causing errors.

    Thank you Goutam!

  • Verified answer
    gdas Profile Picture
    gdas 50,085 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans