web
You’re offline. This is a read only version of the page.
close
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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans