SOLVED

Add hyphen with JSON based on field length

Copper Contributor

I am trying to utilize JSON to conditionally add a hyphen to a single line text column based on the length of the data in the field.  I have validation on the same column that only allows entry of 6 or 7 characters.  If 7 characters are entered the hyphen should display after the 3rd character, if 6 characters are entered it should display after the 2nd character.  This is what I have so far but it doesn't work...

 

 

{
  "$schema": "<a href="https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json" target="_blank">https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json</a>",
  "elmType": "div",
  "children": [
    {
      "elmType": "span",
      "txtContent": {
        "operator": "?",
        "operands": [
          {
            "operator": "==",
            "operands": [
              "@currentField.length",
              "7"
            ]
          },
          {
            "operator": "?",
            "operands": [
              {
                "operator": "+",
                "operands": [
                  "@currentField.0",
                  "@currentField.1",
                  "@currentField.2",
                  "-",
                  "@currentField.3",
                  "@currentField.4",
                  "@currentField.5",
                  "@currentField.6"
                ]
              },
              {
                "operator": "+",
                "operands": [
                  "@currentField.0",
                  "@currentField.1",
                  "-",
                  "@currentField.2",
                  "@currentField.3",
                  "@currentField.4",
                  "@currentField.5"
                ]
              }
            ]
          }
        ]
      }
    }
  ]
}

 

 

 

1 Reply
best response confirmed by cassirk (Copper Contributor)
Solution

@cassirk

figured it out myself.  I was making it way too hard.

 

 

 

 

{
  "$schema": "<a href="https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json" target="_blank">https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json</a>",
	"elmType": "div",
  "txtContent": "=if(@currentField.length != 0,substring(@currentField,@currentField.length-4,0) + '-' + substring(@currentField,@currentField.length-4,8),'')",
}

 

 

 

 

1 best response

Accepted Solutions
best response confirmed by cassirk (Copper Contributor)
Solution

@cassirk

figured it out myself.  I was making it way too hard.

 

 

 

 

{
  "$schema": "<a href="https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json" target="_blank">https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json</a>",
	"elmType": "div",
  "txtContent": "=if(@currentField.length != 0,substring(@currentField,@currentField.length-4,0) + '-' + substring(@currentField,@currentField.length-4,8),'')",
}

 

 

 

 

View solution in original post