Arcade Expression to return non-numeric values in a field for validation attribute rule

1437
6
Jump to solution
03-21-2023 08:29 AM
JasonBessert
New Contributor III

I'm trying to build an Arcade expression for an attribute that will flag features that have non-numeric values in the strings.  Of course, the easier thing to do would be to have the field a long integer/short integer, yet this is over a systematically created field that is text string for the data type, yet the intent of the field for the field to NOT store any attribute values other than numeric

Below is an attribute rule expression built to simply root-out null values 

return !IsEmpty($feature.Name)

 

Trying to replace !IsEmpty with a function helper to sift through the values to look for strings having anything other than numbers.

 

Thanks!

2 Solutions

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Remove the first two lines of code, since the code will end at that first return regardless of what you type in below it. You're correct in stating that it was just an example of how to find a non-numeric value.

You have also check whether the $feature.Name is a number before checking if it's a NaN (line 2).

if (Includes(["PAR", "OVERFL", "SBE"], $feature.Feature )){
    return IsNaN(Number($feature.Name));
} else {
  return true;
}

 

View solution in original post

JasonBessert
New Contributor III

Thanks Ken!!

The only flaw with your rule expression is that I needed to insert an (!) in front of the IsNAN, as the attribute rule expression prompts the user to enter acceptance criteria for features that would pass acceptance.  With having your code from the previous message, it flagged all of the features that had ONLY numeric characters, which was the opposite of what I needed the attribute rule to look for.

if (Includes(["PAR", "OVERFL", "SBE"], $feature.Feature )){
    return !IsNaN(Number($feature.Name));
} else {
  return true;
}

Thanks for all your help

 

View solution in original post

6 Replies
KenBuja
MVP Esteemed Contributor

The Number function will return a number for a string that contains all numeric values ("123" -> 123), but returns a "NaN" for a string that contains non-numeric values ("1a3"). You can use IsNan to test this.

var test = Number("1a3");
return IsNaN(test);

 

JasonBessert
New Contributor III

Thanks, but how would I use it in an expression to enforce a dataset with thousands of records to have an attribute rule flag records that have any letters or non-numeric characters in the string?

It's for someone who wants to make sure all records are 1111111, 12345678, 88888888, etc and not mistakenly 1X111111, 1234567T, A8888888

0 Kudos
KenBuja
MVP Esteemed Contributor

I updated my answer to show how to test for a NaN.

0 Kudos
JasonBessert
New Contributor III

Hmmm,

I just tried your solution, figuring you were giving me part of the coding I needed and baked it into my overall expression:

var test = Number("1a3");
return IsNaN(test);
if(Includes(["PAR", "OVERFL", "SBE"], $feature.Feature )){
    return IsNaN($feature.Name);
} else {
  return true
}

 

Unfortunately, it didn't seem to return any results in a test case.  Is the "var" portion in your coding meant to give an example of what to look for as problematic?  

 

Just trying to apply over the language you shared to my specific database, where a "Name" field is where the NaN values need to be examined over

0 Kudos
KenBuja
MVP Esteemed Contributor

Remove the first two lines of code, since the code will end at that first return regardless of what you type in below it. You're correct in stating that it was just an example of how to find a non-numeric value.

You have also check whether the $feature.Name is a number before checking if it's a NaN (line 2).

if (Includes(["PAR", "OVERFL", "SBE"], $feature.Feature )){
    return IsNaN(Number($feature.Name));
} else {
  return true;
}

 

JasonBessert
New Contributor III

Thanks Ken!!

The only flaw with your rule expression is that I needed to insert an (!) in front of the IsNAN, as the attribute rule expression prompts the user to enter acceptance criteria for features that would pass acceptance.  With having your code from the previous message, it flagged all of the features that had ONLY numeric characters, which was the opposite of what I needed the attribute rule to look for.

if (Includes(["PAR", "OVERFL", "SBE"], $feature.Feature )){
    return !IsNaN(Number($feature.Name));
} else {
  return true;
}

Thanks for all your help