locked
Canada Postal Code RRS feed

  • Question

  • Hi

    I am trying to code to format a textfield in one form of Crm 2011 to accept Canada Postal Code exactly like this Format 'A1A 1A1'.

    I have done coding which creates a space after 3 characters and also called a function fired on onblur event which deletes any input of incorrect format  in the postal code textbox .

    But when i am trying to save , even if the value in postal code textbox is null it is still storing previously given wrong format.

    Can someone help me with code snippets


    Ashok

    Tuesday, August 7, 2012 7:28 AM

All replies

  • Hi

    Try This Code

    function Validate()
     {
     var regEx = /[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/;
    var x=Xrm.Page.getAttribute("address1_postalcode").getValue();
     if(regEx.test(x))
     {
     alert('Valid Postal Code');
     }
     else
     {
     alert('Invalid Postal Code');
     }
     }

    Tuesday, August 7, 2012 9:52 AM
  • Thanks Riaz

    Ashok

    Tuesday, August 7, 2012 2:06 PM
  • Take help here:

    http://sudipanddynamicscrm.blogspot.com/

    I will send you code for Canada postal code as well shortly let me work on this. Mean time take help from above link.

    Thanks.


    Puneet Joshi - Dynamics CRM Developer

    Tuesday, August 7, 2012 4:23 PM
  • If you see the blog the code is something like this :

    function FormatPostalCode(context)
    {
    var oField = context.getEventSource().getValue();
    var tmp = oField;
        if(typeof(oField) != "undefined" && oField != null)
      {
        // check for US ZIP code format
        if(oField.match(/(^\d{5}$)|(^\d{9}$)|(^\d{5}-\d{4}$)/))
        {
            tmp = oField;
                   
    if(tmp.match(/^\d{9}$/))
        {
          tmp = tmp.substr(0,5) + "-" + tmp.substr(5,9);
          context.getEventSource().setValue(tmp);
          return true;
        }
    }
    else
        alert("Zip Code must contain 5 or 9 numbers.");
        //context.getEventSource().setValue("");
             }

    }

    Add something like this in place of US Format and some error handling part and you will be done:

             

    // check for Canadian postal code
    if(oField.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/))

    {
            tmp = oField;
     
    if(tmp.match(/^\d{9}$/))  --- 9 stands for US zip code max digit number like 43220-1234 (I do not know about canada)
        {
          tmp = tmp.substr(0,3) + " " + tmp.substr(3,3););
          context.getEventSource().setValue(tmp);
          return true;
        }

    Let me know if this helps.


    Puneet Joshi - Dynamics CRM Developer

    • Proposed as answer by Puneet Joshi Tuesday, August 7, 2012 6:35 PM
    Tuesday, August 7, 2012 6:34 PM