How do I make Phone number a fixed pattern

If the user enters 9 digits: 01202345678 I need it to be changed to 10202 345678 (5 characters + space + 6 characters)
The problem is to know where to enter the code.
The On Validate is not good as if the field is changed by code, it triggers the code again.
I can run the code after leaving the record bit is it possible to do it as the user is entering more data?
My code works only if the user enters 9 characters and no space but not if they enter the 5 space 6 format, it then errors.

Function: formatphonenumber(phonenumber : Text[30]) : Text[30]
space := COPYSTR(phonenumber,6,1);
IF space <> '' THEN BEGIN
five := COPYSTR(phonenumber,1,5);
six := COPYSTR(phonenumber,6,18);
NewNumber := five + ' ' + six;
EXIT(NewNumber)
END ELSE
EXIT(phonenumber);
Experience is what you get when you hoped to get money

Answers

  • lubostlubost Member Posts: 628
    Depending on NAV version you can use:
    1. Regular expression to match user input against your format (let users to enter phone number in right format)
    2. Use addon to show required pattern and lead users to fill it
  • waitwait Member Posts: 53
    lubost wrote: »
    1. Regular expression to match user input against your format (let users to enter phone number in right format)

    I like the idea of using regular expression to do this, I would think OnValidate would be the place to do this, I don't understand why that would not work.

    There are plenty of instructions online on how to format phone numbers in .NET , for instance I would think the c# example in the following link would be plug and play in Nav.

    https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s02.html
  • lubostlubost Member Posts: 628
    OnValidate trigger is right place, but you would create a function, because check and format phone number is a common functionality for multiple fields and is better to call a function then drop the same code to each trigger.
Sign In or Register to comment.