3

I want to add a contact number field, so I am using the inbuilt number field, but it is separating the number with comma, and I want hyphen as a separator.

E.g: Format now - 1,234,567,890.

Format required - 1-234-567-890

Is it possible using OOTB approach?

1
  • You want this in a view or modifying the value itself? For example, you just want to display as - 1-234-567-890 in some list view. Then you can use CSR – Atish Dipongkor - MVP Nov 25 '15 at 14:20
3

As Atish mentioned, CSR is a nice way to change the "displayForm" or the list itself.

I tied something little different quickly.

You could use a calculated column and replace the "," with "-".

But the numberlength should be always the same. Not perfect, but maybe also a possible way for your requirement. Here how I did it:

Listview with calculated column:

enter image description here

Formula: =TEXT(Number,"0-000-000-000")

enter image description here

1
  • typo in Formular. numberlength should be always the same. as it's a contact no. – Atish Dipongkor - MVP Nov 25 '15 at 14:52
1

this has already been answered:

Column validation for US phone number format

DVWP that you add to the new form

<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript" ></script>
<script language="javascript" src="http://jquery-joshbush.googlecode.com/files/jquery.maskedinput-1.2.2.min.js" type="text/javascript" ></script>
<script language="javascript" type="text/javascript">
jQuery(function($){
   $("input[title='Phone']").mask("9-999-999-999");
});
</script>

you need to also add it to the edit form

1

Every possible solution has been added. So why not CSR. Execute following code in the OnPreRender

For AllItems view

ctx.ListData.Row.forEach(function(item){
  item.Number= item.Number.replace(/,/g,"-");
  });

For DisplayForm

ctx.ListData.Items[0].Number= ctx.ListData.Items[0].Number.replace(/,/g,"-"); 

Use Cisar for CSR development. Benefits of Cisar

  1. It will generate the boilerplate of JSLink
  2. Live code edit in console
  3. Live test
  4. Auto deployment
5
  • Just curious.. have you checked this.. I presume Number is a .. Number .. and does not contain commas.. Its SharePoint who adds the formatting – Danny '365CSI' Engelman Nov 25 '15 at 15:34
  • 1
    I have checked it in my SPO site. It contains comma. In ctx.ListData.Row, there are two properties like Number and Number. Number contains the formatted string and Number. contains the only number. PS: Number is my column name also @DannyEngelman – Atish Dipongkor - MVP Nov 25 '15 at 15:43
  • 2
    Indeed.. CurrentItem gives FieldName as formatted string, and FieldName. (with dot) as unformatted number (as a string)... I hate 🚾 Microsoft programmers sometimes... that means you can only get the value with string-notation: CurrentItem["Number."] – Danny '365CSI' Engelman Nov 25 '15 at 15:46
  • 2
    PS. You should not do it in the OnPreRender.. It will break any (later added) code that relies on OOB functionality. A View Template for your Number field is a better place...just as much code – Danny '365CSI' Engelman Nov 25 '15 at 15:55
  • Yeah... It means I am modifying ctx.ListData.Row itself. I hope OP will do it if he is interested. – Atish Dipongkor - MVP Nov 25 '15 at 16:01
0

+1 to Patrick for using the (lesser known) TEXT function to format a number

Here is the Calculated Formula for Number lengths 1 to 10, in a VIEW or DisplayForm

=TEXT(123456
      ,CHOOSE(
              LEN(123456)
              ,"0"
              ,"00"
              ,"000"
              ,"0-000"
              ,"00-000"
              ,"000-000"
              ,"0-000-000"
              ,"00-000-000"
              ,"000-000-000"
              ,"0-000-000-000"))

Replace both 123456 with your Number field reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.