0

Phone Number Formatting?

When typing a phone number in the phone number field (or even text field, whichever is possible), how do I set an automatic phone number format? I would like my format to be (555) 555-5555. 

30 replies

null
    • Birger_H
    • 6 yrs ago
    • Reported - view

    This is not yet possible.

    Birger

    • sjdixey
    • 6 yrs ago
    • Reported - view

    You could try something like:

    let Code := substring(Phone, 0, 3);
    let Num := substring(Phone, 3, 9);
    Phone := "(" + Code + ")" + " " + Num

    in the trigger after update box ? The above example changes 012345678 to (012) 345678 ...

    Note that the Phone datatype has to be Text in this method.

    • David_Gyenes
    • 6 yrs ago
    • Reported - view

    Doesn't have to be Text you still can do in Phone field. This would be your code to get the exact you want:

    let Area := substring(Number, 0, 3);
    let Num := substring(Number, 3, 6);
    let Num2 := substring(Number, 6, 10);
    Number := "(" + Area + ")" + " " + Num + "-" + Num2

    If you mess up the entring the phone number you have to delete and retype the whole again otherwise it would format the already formatted form.

    • nas
    • 6 yrs ago
    • Reported - view

    Copied and pasted... 

    let Area := substring(Number, 0, 3);
    let Num := substring(Number, 3, 6);
    let Num2 := substring(Number, 6, 10);
    Number := "(" + Area + ")" + " " + Num + "-" + Num2

    ...into "trigger after update" box for "Phone" field, but receiving message "File not found: Number at Line 1, column 28"

    • Leonid_Semik
    • 6 yrs ago
    • Reported - view

    In your case:

    —-

    let Area := substring(Phone, 0, 3);
    let Num := substring(Phone, 3, 6);
    let Num2 := substring(Phone, 6, 10);
    Phone := "(" + Area + ")" + " " + Num + "-" + Num2

    —-

     

    Leo

    • Halio
    • Halio
    • 5 yrs ago
    • Reported - view

    @Birger has @Moore's request for automatic phone number formatting been added yet?

    • Halio
    • Halio
    • 5 yrs ago
    • Reported - view

    "Tigger after update" solutions are not always the best workarounds as those change the actual data and do not just visually display the phone number in a different way. 

    • David_Gyenes
    • 5 yrs ago
    • Reported - view

    This is published in the English Webinars...

    try this code in a phone field, trigger after update:

    In this case the phone field name is: 'Format (000) 000-0000'. Does't matter what you enter to the field it will take out the first 10 number you enter and format that based on the the lenght of the numbers. This formula works for US phone numbers without country code!

    let myNumber := trim(text('Fromat (000) 000-0000'));
    let p := length(myNumber);
    let myCars := "";
    var myStart := 0;
    var myEnd := 1;
    for i in range(0, p) do
    var myNum := substring(myNumber, myStart, myEnd);
    if myNum = "0" or myNum = "1" or myNum = "2" or myNum = "3" or myNum = "4" or myNum = "5" or myNum = "6" or myNum = "7" or myNum = "8" or myNum = "9" then
    myCars := myCars + myNum;
    myStart := myStart + 1;
    myEnd := myEnd + 1
    else
    myStart := myStart + 1;
    myEnd := myEnd + 1
    end
    end;
    if length(myCars) < 1 then
    'Fromat (000) 000-0000' := null
    else
    if length(myCars) < 4 then
    'Fromat (000) 000-0000' := myCars
    else
    if length(myCars) < 7 then
    let Area := substring(myCars, 0, 3);
    let Num := substring(myCars, 3, 6);
    'Fromat (000) 000-0000' := "(" + Area + ")" + " " + Num
    else
    let Area := substring(myCars, 0, 3);
    let Num := substring(myCars, 3, 6);
    let Num2 := substring(myCars, 6, 10);
    'Fromat (000) 000-0000' := "(" + Area + ")" + " " + Num + "-" + Num2
    end
    end
    end

    • Halio
    • Halio
    • 5 yrs ago
    • Reported - view

    @David, thank you! This works well for my needs. I still hope a built in version with some modifiers will be released that would make it easier to quickly make changes.
    Could you post the link to the webinar please.

    • Halio
    • Halio
    • 5 yrs ago
    • Reported - view

    What would I need to add/change if I would like to add a +00 country code to the above script?

    • David_Gyenes
    • 5 yrs ago
    • Reported - view

    Your welcome. Please send us an email support@nioxus.com

    • Ben.2
    • 4 yrs ago
    • Reported - view

    Hello Halio, I think you would need to add some sort of "if" function to check the start of the number. In the US, it is not permitted for an area code to start with "0" so I'm thinking of trying to tweak the formula to check the entered string for a 1 or 0 before formatting. If it finds a 1 or 0 I think I will just ask it to leave the number as is, if not, then I will have it run the formula... I'll post below if I get it working...

    • Halio
    • Halio
    • 4 yrs ago
    • Reported - view

    Thanks Art. Looking forward to your solution!

    • Sean
    • 4 yrs ago
    • Reported - view

    This code does the same thing and should be easier to modify. It strips any formatting and if the length of the number isn't at least 10 digits it will set the field to null. The code currently accepts phone numbers with 1 or 2 digit country codes.

     

    let pNumLength := length(replacex(Phone, "\D", "g", ""));
    switch pNumLength do
    case 10:
    Phone := replacex(Phone, "(\d{3})(\d{3})(\d{4})", "", "($1) $2-$3")
    case 11:
    Phone := replacex(Phone, "(\d{1})(\d{3})(\d{3})(\d{4})", "", "+$1 ($2) $3-$4")
    case 12:
    Phone := replacex(Phone, "(\d{2})(\d{3})(\d{3})(\d{4})", "", "+$1 ($2) $3-$4")
    default:
    Phone := null
    end

    • Leonid_Semik
    • 4 yrs ago
    • Reported - view

    Great!

    • Sean
    • 4 yrs ago
    • Reported - view

    I made a mistake with the previous code. It will strip non-numeric characters for testing the length, but it didn't update the phone number string. The following code fixes that.

     

    let phnNum := replacex(Phone2, "\D", "g", "");
    switch length(phnNum) do
    case 10:
    Phone2 := replacex(phnNum, "(\d{3})(\d{3})(\d)", "", "($1) $2-$3")
    case 11:
    Phone2 := replacex(phnNum, "(\d{1})(\d{3})(\d{3})(\d)", "", "+$1 ($2) $3-$4")
    case 12:
    Phone2 := replacex(phnNum, "(\d{2})(\d{3})(\d{3})(\d)", "", "+$1 ($2) $3-$4")
    default:
    Phone2 := null
    end

    • Ben.2
    • 4 yrs ago
    • Reported - view

    sean, your code looks great although I don't like the idea of the field setting to null if it doesn't get 10 digits. For my use, with a variety of international numbers in my database, but with mostly US numbers, I wanted  a solution that would be forgiving if I typed a "+" or 00 or both or none and just entered 10 digits. Also, since I am not the most precise person when I'm doing data entry, I thought it best if I left in a fallback option if I mistakenly dropped a digit, a system where I could go back and just type the single missing digit at the end and the formatting would take over. I'm still tweaking, the formula, but it allows one to even change between "-" and "." or whatever separator one wants for the break between the last two sets of numbers. My only regret is that I could not find an elegant way to split the digits from any punctuation or letters. I'd love a built-in code like "isdigit" or "isletter" to define character types.

    • Ben.2
    • 4 yrs ago
    • Reported - view

    let input := trim(text(TestPhone));
    let break := "-";
    let p := length(input);
    let init := substr(input, 0, 1);
    if init = "0" or init = "1" or init = "2" or init = "3" or init = "4" or init = "5" or init = "6" or init = "7" or init = "8" or init = "9" or init = "(" then
    init := ""
    else
    init := substr(input, 0, 1)
    end;
    let digits := "";
    var myStart := 0;
    var myEnd := 1;
    for i in range(0, p) do
    var myNum := substring(input, myStart, myEnd);
    if myNum = "0" or myNum = "1" or myNum = "2" or myNum = "3" or myNum = "4" or myNum = "5" or myNum = "6" or myNum = "7" or myNum = "8" or myNum = "9" then
    digits := digits + myNum;
    myStart := myStart + 1;
    myEnd := myEnd + 1
    else
    myStart := myStart + 1;
    myEnd := myEnd + 1
    end
    end;
    let q := length(digits);
    if q < 1 then
    TestPhone := null
    else
    if q < 4 then
    TestPhone := digits
    else
    if q < 7 then
    let Area := substring(digits, 0, 3);
    let Num3 := substring(digits, 3, 6);
    TestPhone := "(" + Area + ") " + Num3
    else
    if q < 10 then
    let Area := substring(digits, 0, 3);
    let Num3 := substring(digits, 3, 6);
    let Num4 := substring(digits, 6, q);
    TestPhone := "(" + Area + ") " + Num3 + break + Num4
    else
    let prefix := substring(digits, 0, q - 10);
    let Area := substring(digits, q - 10, q - 7);
    let Num3 := substring(digits, q - 7, q - 4);
    let Num4 := substring(digits, q - 4, q);
    TestPhone := init + prefix + "(" + Area + ") " + Num3 + break + Num4
    end
    end
    end
    end

    • Ben.2
    • 4 yrs ago
    • Reported - view

    Sean, your code is very elegant, and I didn't realize while reading up on NX script that it accepted character class formatting via replacex and extractx commands... I'll have to keep tweaking

    • Sean
    • 4 yrs ago
    • Reported - view

    Art, The code accepts 10, 11 and 12 digit phone numbers and automatically adds "+" for 11 and 12 digit numbers. You could type "+" if you like, but it would be removed before the formatting took place. I didn't intend for it to be an end-all solution so feel free to modify it if you like. Another option would be to base the formatting on a country field.

     

    extractx(), replacex(), splitx() and testx() are functions that let you use regular expressions in Ninox.

    • Ben.2
    • 4 yrs ago
    • Reported - view

    Thanks for the info. I'm very amateur at scripting/coding, but I'm finding NX really easy to use, and the application in general extremely useful for my purposes. I'm using this phone number script to get my feet wet before I actually import my full data and go live with my personal system. Thank you very much for the info, and I'm going to play around with your code and see what I can learn from it as well. I have a feeling I'm going to be using a lot of the functions that allow regular expressions in the future.

    • Ben.2
    • 4 yrs ago
    • Reported - view

    Halio, below is my latest code (with lots of ideas taken from Sean). It adds a "+" to any string of numbers over 10, adds the (###) ###-### formatting to any string of 10+ numbers, and completes any string under 10 digits with "x" as a placeholder for the last digits so you can quickly see that you are missing digits in the string. For you to have every number start with "+00" , and not any other country code, this code would have to be changed to add in the zeros, which would then require a bit of tweaking to avoid adding them if they are already there.

    let sym := "+";
    let pad := "x"
    let digits := replacex(Phone, "\D", "g", "");
    let ln := length(digits);
    let pre := ln - 10;
    let split := "(\d{" + pre + "})(\d{3})(\d{3})(\d)";
    if pre < 1 then sym := "" else sym := sym end;
    let format := sym + "$1 ($2) $3-$4";
    if ln < 10 then
    Phone := rpad(Phone, 10, pad)
    else
    Phone := replacex(digits, split, "", format)
    end

    • Ben.2
    • 4 yrs ago
    • Reported - view

    I put the symbol and padding variables at the top so you can change them without having to hunt through the code.

    • Sean
    • 4 yrs ago
    • Reported - view

    Looks like you're getting the hang of it Ben. You can omit else sym := sym since sym was already defined in the beginning.

    • Ben.2
    • 4 yrs ago
    • Reported - view

    Oh, great ! Thanks for the tip. Slightly off topic, but related to the codes here: is it possible to make a reference like let fieldname:= x at the beginning and then use if y then x:=somestring  later on in the code. This way you could make a code that could be copied and pasted into other fields without having to go through and change the name in each instance, just once in the beginning. This is of course a full question in its own right, but it would actually apply to every one of the codes given here as a reply to the op

Content aside

  • 4 yrs agoLast active
  • 30Replies
  • 8455Views