2009-10-05

Mask the phone fields

I was need some mask on the "mobilephone" field in contact card. So I was combine some google search result with my experience and got next code:
//base maskphone function 
window.applyMask = function()
{
    // var of fields wich must be masked
    var ele = document.getElementById("mobilephone");

var FormatPhoneHandler=function()
    {
      var PH = this.value;
      var re = /\D/g;
      // +7 - this is country code of Russia, so this is decision only for one country
      PH = PH.replace("+7","");
      PH = PH.replace(re,"");
     
      if(PH.length <1)
      {
          this.value = "";
      }
      else if(PH.length <= 3)
      {
            this.value = "+7 (" + PH;
      }
      else if(PH.length > 3 && PH.length <= 6)
      {
            this.value = "+7 (" + PH.substring(0,3) + ") " + PH.substring(3,6);
      }
      else if(PH.length > 6)
      {
            this.value = "+7 (" + PH.substring(0,3) + ") " + PH.substring(3,6) + "-" + PH.substring(6,10);
      }
    }

    ele.onkeyup = FormatPhoneHandler;
}

// turn it on
applyMask();

Place this code in onLoad event in the entity wich you need and don't forget change names of the field for the right one.

Комментариев нет: