Format a zip code into USA format using javascript in dynamics crm 2011
-
14 Mei 2012 16:59
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
Semua Balasan
-
14 Mei 2012 17:06
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
- Diedit oleh _Vikram 14 Mei 2012 17:08
-
14 Mei 2012 17:28
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
-
15 Mei 2012 14:00
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
- Ditandai sebagai Jawaban oleh Puneet Joshi 15 Mei 2012 14:01
- Tanda sebagai Jawaban dihapus oleh Puneet Joshi 15 Mei 2012 15:12
- Diedit oleh Puneet Joshi 15 Mei 2012 15:12
- Ditandai sebagai Jawaban oleh Puneet Joshi 15 Mei 2012 15:12