ATTRIBUTE CONSTRAINT RULE (Limit Characters)

656
2
Jump to solution
06-01-2023 08:09 AM
Labels (1)
EDUARDOARDILARINCON
New Contributor III

Hi Everyone!

I'm trying to create a constraint attribute rule that limits the types of characters in a field, I've tried using the find() function which returns -1 in case the character is not found within the typed text. This workflow works perfect when I only search for one character/word, unfortunately I need to limit the field to certain specific characters regardless of their order , length or if they are repeated within the text string (eg A, B, C. and no matter if the field contains AA, ACB, BAC, BBAC). I tried using find() in conjunction with the operator || but i failed miserably 😂😂
So here´s the piece of code I tried to use:

 

 

var test

test= find(('A'||'B'||'C'),$feature.name)

if (test>=0) {
return true;
}
else {
return false;
}

 

 

 

I appreciate so much your comments/suggestions.

Thank you all!

0 Kudos
1 Solution

Accepted Solutions
EDUARDOARDILARINCON
New Contributor III

Hi Josh, thank you for your reply.

Before creating the attribute restriction rule, I decided to load the script into an attribute calculation rule pointing to another field, in order to see what values ​​the script was generating (just changing true to 'OK' and false to 'NO').

I've playing around some time with your script but it didn't work, I think is because the true/false values are inverted (if I put 'D' as input the script will skip the if clause and 'test' will take the input as valid), I switched them but it didn't work either. So, again following the code and debugging it in my brain (pretending I knew what I was doing 😂😂) I realized that it only worked when you put a invalid character at the very beginning, but if that invalid character was preceded by one or more (like 'ABCD') valid characters the srcipt will skip the 'if()' again and keeps the text as valid. 

After a few hours of scripting philosophy 🤣 I thought that instead of compairing every allowed character with my input text it would be better to compare each character of the input with the allowed ones; when a character of my input doesn't match the 'finders' array the if clause change 'test' value to 'NO' (or false), so the script ended like this:

// default to OK
var test = 'OK'

// array of things to search for
var finders = [
    'A',
    'B',
    'C'
]

//compairing each character in 'name' field with allowed characters in 'finders'
for (var f in $feature.name) {
    //if any character in 'name' field doesn't match with allowed characters in 'finders' variable test changes to 'NO'
    if (find($feature.name[f], finders) == -1) {
        test = 'NO'
        
        // as soon as a match is found, no need to keep checking
        break
    }
}

return test

 

As far as I tested my script, it appears to work just fine, but I would prefer if you check it please, because I'm quite new writing code so I don't want the script has silly newbie code issues 😅.

 

Once again, thanks a lot, not only for your reply but showing me the rigth path to follow.

 

🤙🤙🤙

- Eduardo Ardila

 

View solution in original post

0 Kudos
2 Replies
jcarlson
MVP Esteemed Contributor

Yeah, the find function can't handle multiple input strings. (Same with replace, too.)

You can search multiple things easily enough using an array and a loop.

 

// default to true
var test = true

// array of things to search for
var finders = [
    'A',
    'B',
    'C'
]

for (var f in finders) {
    if (find(finders[f], $feature.name) != -1) {
        test = false
        
        // as soon as a match is found, no need to keep checking
        break
    }
}

return test

 

- Josh Carlson
Kendall County GIS
EDUARDOARDILARINCON
New Contributor III

Hi Josh, thank you for your reply.

Before creating the attribute restriction rule, I decided to load the script into an attribute calculation rule pointing to another field, in order to see what values ​​the script was generating (just changing true to 'OK' and false to 'NO').

I've playing around some time with your script but it didn't work, I think is because the true/false values are inverted (if I put 'D' as input the script will skip the if clause and 'test' will take the input as valid), I switched them but it didn't work either. So, again following the code and debugging it in my brain (pretending I knew what I was doing 😂😂) I realized that it only worked when you put a invalid character at the very beginning, but if that invalid character was preceded by one or more (like 'ABCD') valid characters the srcipt will skip the 'if()' again and keeps the text as valid. 

After a few hours of scripting philosophy 🤣 I thought that instead of compairing every allowed character with my input text it would be better to compare each character of the input with the allowed ones; when a character of my input doesn't match the 'finders' array the if clause change 'test' value to 'NO' (or false), so the script ended like this:

// default to OK
var test = 'OK'

// array of things to search for
var finders = [
    'A',
    'B',
    'C'
]

//compairing each character in 'name' field with allowed characters in 'finders'
for (var f in $feature.name) {
    //if any character in 'name' field doesn't match with allowed characters in 'finders' variable test changes to 'NO'
    if (find($feature.name[f], finders) == -1) {
        test = 'NO'
        
        // as soon as a match is found, no need to keep checking
        break
    }
}

return test

 

As far as I tested my script, it appears to work just fine, but I would prefer if you check it please, because I'm quite new writing code so I don't want the script has silly newbie code issues 😅.

 

Once again, thanks a lot, not only for your reply but showing me the rigth path to follow.

 

🤙🤙🤙

- Eduardo Ardila

 

0 Kudos