I'm using this function to phone mask and works almost perfectly.
function mask(o, f)
{
v_obj = o;
v_fun = f;
setTimeout("execmask()", 1)
};
function execmask()
{
v_obj.value = v_fun(v_obj.value)
};
function mphone(v){
v=v.replace(/\D/g,"");
v=v.substring(0, 11);
v=v.replace(/^(\d{2})(\d)/g,"(OXX$1) $2");
v=v.replace(/(\d)(\d{4})$/,"$1-$2");
return v;
}
Here I run the mask in the text field:
<input type="text" id="phone" name="phone" onkeypress="mask(this, mphone);" onblur="mask(this, mphone);" />
The problem is that I need to change this part of the code (OXX$1) for (0XX$1).
Current situation with 9 digit: (OXX99) 99999-9999.
Current situation with 8 digit: (OXX99) 9999-9999.
The correct formatting that I need with 9 digit: (0XX99) 99999-9999
The correct formatting that I need with 8 digit: (0XX99) 99999-9999
The amount of 8 or 9 digit is the choice of the user.
If I change the character 'O' for '0', causes an error in the mask.
Please, help!
http://jsfiddle.net/BBeWN/
I love this function and I use it all the time. I've added 2 other masks if anyone needs them. I understand that they don't directly answer the question, but they are super useful.