I have two string which should be put together into one string. First string is a input value and second string is a pattern how the first string should look.
Here is the example –
Input string( var val ) – 9165678823
Patter string( var mask ) – (999)999-9999
Output string should look like( var startVal ) – (916)567-8823
I have tried working out and this is my code
var val = $(control).data("loadMaskValue"); // Input Value
var mask = $(control).attr("mask"); //Masking Pattern
var startVal = "";
var j = 0;
for (var i = 0; i < mask.length; i++) {
var c = mask.charAt(j);
if (c == '9' || c == 'X' || c == 'A') { //Checks the char is normal char
startVal += val.charAt(j);
}
else {
startVal += c; //Inserts the special char to string like ( ) -
startVal += val.charAt(j);
}
j = startVal.length;
}
The problem with this code is it misses one number in between. The result of this code is
startValue – (965)688-2.
PLease help me.
Here’s a slightly simpler implementation:
Make sure that
inputhas been stripped of all whitespace at the beginning and end.Demo: http://jsfiddle.net/qWtjk/