Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Formatting other numbers

Questions related to using nuBuilder Forte.
Post Reply
tonyd
Posts: 68
Joined: Sun Mar 04, 2018 6:38 pm

Formatting other numbers

Unread post 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.
TonyD
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Formatting other numbers

Unread post 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.
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Formatting other numbers

Unread post 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
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Formatting other numbers

Unread post 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
tonyd
Posts: 68
Joined: Sun Mar 04, 2018 6:38 pm

Re: Formatting other numbers

Unread post 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.
TonyD
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Formatting other numbers

Unread post by admin »

.
Post Reply