Page 1 of 1

Formatting other numbers

Posted: Thu Mar 22, 2018 1:27 pm
by tonyd
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.

Re: Formatting other numbers

Posted: Thu Mar 22, 2018 2:28 pm
by toms
I use onblur (custom code) to format/convert a phone number into international format.
The onblur event occurs when an object loses focus. A JavaScript function then formats the field's value and
writes the new value back to the field.

Re: Formatting other numbers

Posted: Fri Mar 23, 2018 3:25 am
by admin
TonyD,

Sorry but there won't be any new functions added to nuBuilder Forte for doing this, but as toms mentioned, you can do it yourself with Javascript.

Steven

Re: Formatting other numbers

Posted: Fri Mar 23, 2018 4:40 am
by admin
TonyD,

This is something we did in nuBuilder 3 that might help...

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);


}

BTW I copied and modified this ( I don't understand regex).



Steven

Re: Formatting other numbers

Posted: Fri Mar 23, 2018 2:08 pm
by tonyd
Here is what I came up with

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;
}
and then I use it with a simple call in the JavaScript onblur event

Code: Select all

var phone = nuFormatString($(this)[0].value,"Phone",10);
if (phone !== false) {
    $(this)[0].value = phone;
    $(this).change();
}
I tried to make it expandable to other format ideas as well.

Thanks for the help and suggestions.

Re: Formatting other numbers

Posted: Fri Mar 23, 2018 7:07 pm
by admin
.