Formatting other numbers
Posted: Thu Mar 22, 2018 1:27 pm
Is there a way to format a phone number or social security number in nuBuilder? If not, is there a consideration to add such functionality in the future?
Thanks.
Thanks.
Code: Select all
function nuFormatMobile(t){
if(t.value.length != 10){
alert('Must have 10 numbers');
t.value = '';
return;
}
t.value = t.value.replace(/(\d{4})(\d{3})(\d{3})/, '$1 $2 $3');
t.value = String(t.value).substr(0,12);
}
Code: Select all
function nuFormatString(s, f, l){
//which format?
switch (f) {
case "SSN":
case "ssn":
// check to see if the number is already formatted correctly
if (s.search(/\d{3}-\d{2}-\d{4}/) === -1) {
//normalize string and remove all unnecessary characters
s = s.replace(/[^\d]/g, "");
//check if number length is OK
if (s.length == l) {
//reformat and return phone number
return s.replace(/(\d{3})(\d{2})(\d{4})/, "$1-$2-$3");
}
}
break;
case "Phone":
case "phone":
// check to see if the number is already formatted correctly
if (s.search(/\(\d{3}\)\s\d{3}-\d{4}/) === -1) {
//normalize string and remove all unnecessary characters
s = s.replace(/[^\d]/g, "");
//check if number length is OK
if (s.length == l) {
//reformat and return phone number
return s.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
}
}
default:
}
return false;
}
Code: Select all
var phone = nuFormatString($(this)[0].value,"Phone",10);
if (phone !== false) {
$(this)[0].value = phone;
$(this).change();
}