Constraint rule to only input text formatted as ##-#####-##

505
2
Jump to solution
09-16-2021 01:15 PM
NikeshPatel
New Contributor II

Hi,

I've been struggling to find a reference to enable a constraint attribute rule that only allows users to input  text as "##-#####-##" for a specific text field. I'm happy to have the user input this manually or have the dashes automatically put in. 

I tried the below:

 

text($feature.accountname, '##-#####-##')

 

I'm avoiding creating a new field to perform a calculation rule as I can't really change the schema at this current time due to connected services. 

Would appreciate any and all help

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Don't do it as constraint rule, but as calculation rule on accountname:

// Calculation attribute rule
// Field acccountname
// Triggers: Insert, Update
if(IsEmpty($feature.accountname)) {
  return null
}
var input = Replace($feature.accountname, "-", "")
return Left(input, 2) + "-" + Mid(input, 2, 5) + "-" + Right(input, 2)

 

To ensure that this calculates correct values, you can do a constraint rule that checks for the length of the string:

// Constraint attribute rule
// rejects the edit if there are not exactly 9 characters
var input = Replace($feature.accountname, "-", "")
return Count(input) == 9

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

Don't do it as constraint rule, but as calculation rule on accountname:

// Calculation attribute rule
// Field acccountname
// Triggers: Insert, Update
if(IsEmpty($feature.accountname)) {
  return null
}
var input = Replace($feature.accountname, "-", "")
return Left(input, 2) + "-" + Mid(input, 2, 5) + "-" + Right(input, 2)

 

To ensure that this calculates correct values, you can do a constraint rule that checks for the length of the string:

// Constraint attribute rule
// rejects the edit if there are not exactly 9 characters
var input = Replace($feature.accountname, "-", "")
return Count(input) == 9

Have a great day!
Johannes
NikeshPatel
New Contributor II

Thank you!  I did not think to use replace in a calculation.

0 Kudos