i want mask javascript variable value.basically getting phone number fields value in variable , after getting want mask in other format.
i have tried jquery mask plugin mask control's string format.but want mask string variable.
ex:
let have phone string "0000000000"
, storing in variable :-
var number ="0000000000"
and after want mask in other formats.like-
1) number = number.mask('000-0000'); 2) number = number.mask('(000) 000-0000');
there similar question (javascript phone mask text field regex).
you can using regular expressions (see code working @ http://jsfiddle.net/bbewn/45/):
var value = '1234567'; var formatted = value.replace(/^(\d{3})(\d{4}).*/, '$1-$2');
notice each part of regular expression wrapped within parenthesis, (\d{3})
, (\d{4})
in example above, can referenced build formatted text string using $1
, $2
respectively.
if have n parts of regular expression wrapped within parenthesis, able reference them build formatted text string $1
, $2
, ..., $n
respectively.
so, other format mention ((000) 000-0000), code this:
var formatted = value.replace(/^(\d{3})(\d{3})(\d{4}).*/, '($1) $2-$3');
you can find great javascript regular expressions reference @ https://developer.mozilla.org/en-us/docs/web/javascript/guide/regular_expressions
Comments
Post a Comment