Format a zip code into USA format using javascript in dynamics crm 2011
-
Monday, May 14, 2012 4:59 PM
Hello Everyone,
I am trying to format a postal code into US Format using JScript for Dynamics crm 2011.
Following the code to format. I am not sure the Regex that i am using is correct or not. It is not giving me any error.
I created a web resource in CRM with the following Javascript code:
function FormatPostalCode(context)
{
var oField = context.getEventSource().getValue();
var sTmp;
if(typeof(oField) != "undefined" && oField != null)
{
// check for US ZIP code
if(oField.match (/^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/))
{
context.getEventSource().setValue(oField);
return true;
}
//alert("Incorrect ZIP/Postal Code format.");
}
}Reference it on the Zipcode’s OnChange() event. Also selected the “Pass execution context as first parameter” option.
The Code is not throwing any error, not even formatting it.
Can somebody help me out please.
Puneet Joshi
All Replies
-
Monday, May 14, 2012 5:06 PM
Hi,
Refer to http://docs.limesurvey.org/Using+regular+expressions&structure=Deutsche+Anleitung+f%C3%BCr+LimeSurvey#US_Postal_Codes:
http://blog.webfortis.com/post/2012/04/07/MS-CRM-2011-Field-Formatting-and-Regular-Expression-Objects-with-JavaScript.aspx
- Edited by _Vikram Monday, May 14, 2012 5:08 PM
-
Monday, May 14, 2012 5:28 PM
Well, I went through these websites before you suggested, i could not resolve it myself.
I thought i need second set of eyes to look at it and point me where i am making mistake.
Thanks for the reply.
Puneet Joshi
-
Tuesday, May 15, 2012 2:00 PM
Fixed the code using this piece
function FormatPostalCode(context)
{
var oField = context.getEventSource().getValue();
var sTmp = oField;
if(typeof(oField) != "undefined" && oField != null)
{
// check for US ZIP code
if(oField.match(/(^\d{5}$)|(^\d{9}$)|(^\d{5}-\d{4}$)/))
{
sTmp = oField;
if(sTmp.match(/^\d{9}$/))
{
sTmp = sTmp.substr(0,5) + "-" + sTmp.substr(5,9);
context.getEventSource().setValue(sTmp);
return true;
}
}
else
alert("Zip Code must contain 5 or 9 numbers.");
//context.getEventSource().setValue("");
}
}
Puneet Joshi
- Marked As Answer by Puneet Joshi Tuesday, May 15, 2012 2:01 PM
- Unmarked As Answer by Puneet Joshi Tuesday, May 15, 2012 3:12 PM
- Edited by Puneet Joshi Tuesday, May 15, 2012 3:12 PM
- Marked As Answer by Puneet Joshi Tuesday, May 15, 2012 3:12 PM