Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  178] [ 3]  / answers: 1 / hits: 34339  / 11 Years ago, thu, april 4, 2013, 12:00:00

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) to (0XX$1).


Current situation:




















No. Of Digits Input Field
9 digit (OXX99) 99999-9999
8 digit (OXX99) 9999-9999


The correct formatting that I need:




















No. Of Digits Input Field
9 digit (0XX99) 99999-9999
8 digit (0XX99) 9999-9999


The amount of 8 or 9 digits is the choice of the user.


Changing O to 0, causes an error in the mask.


More From » regex

 Answers
10
function mask(o, f) {
setTimeout(function () {
var v = f(o.value);
if (v != o.value) {
o.value = v;
}
}, 1);
}

function mphone(v) {
var r = v.replace(/D/g,);
r = r.replace(/^0/,);
if (r.length > 10) {
// 11+ digits. Format as 5+4.
r = r.replace(/^(dd)(d{5})(d{4}).*/,(0XX$1) $2-$3);
}
else if (r.length > 5) {
// 6..10 digits. Format as 4+4
r = r.replace(/^(dd)(d{4})(d{0,4}).*/,(0XX$1) $2-$3);
}
else if (r.length > 2) {
// 3..5 digits. Add (0XX..)
r = r.replace(/^(dd)(d{0,5})/,(0XX$1) $2);
}
else {
// 0..2 digits. Just add (0XX
r = r.replace(/^(d*)/, (0XX$1);
}
return r;
}


http://jsfiddle.net/BBeWN/


[#79130] Tuesday, April 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayla

Total Points: 681
Total Questions: 102
Total Answers: 108

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
tayla questions
Fri, Mar 5, 21, 00:00, 3 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;