Can you use Regular Expressions to validate input?

I need to verify text input on blur that the value is alphanumeric and has a min & max length of 10. Been searching through the docs and community but have not had any hits on Regular Expression or RegEx.

Hi, @tsnowden!

RegEx typically refers to a very specific syntax, which Airkit does not parse, but Airscript can be used to define a lot of the same functionality.

For instance, say the text input was called text_input. You could use the function LENGTH combined with the function IF to check if it was 10 characters long, like so:

IF(LENGTH(text_input) = 10, "This is a valid string", "This is not a valid string.")

The alphanumeric check is a bit tricker: all text input is saved as a string, but we don’t have an out-of-the-box way to confirm that string doesn’t contains (or doesn’t contain) any punctuation or special characters. That said, the STRING_FIND function lets you look for individual characters within a string, and you could make a UDF that nests STRING_FIND calls to confirm some base_string doesn’t have any non-alphanumeric characters you really want to search for. For instance, the following function takes some base_string and returns TRUE if the base string doesn’t contain a space, period or exclamation point, FALSE otherwise:

IF(
  STRING_FIND(base_string, " ") != -1,
  FALSE,
  IF(
    STRING_FIND(base_string, ".") != -1,
    FALSE,
    IF(
      STRING_FIND(base_string, "!")
        != -1,
      FALSE,
      TRUE
    )
  )
)

Hope that helps you get started!

Thank you @Chandra_Bergmann!

To expand on this, I’m going to take what @kyung built for this question here:

So there’s two UDFs you can create here:

Char#IsLetter

LENGTH(ch) = 1 AND ((ch >= "A" AND ch <= "Z") OR (ch >= "a" AND ch <= "z"))

Char#IsNumber

LENGTH(ch) = 1 AND (ch >= "0" AND ch <= "9")

These allow you to detect if the passed string is a number or a letter.

From here, you can then split the desired string, into an array and check to see if all values are alphanumeric.

An easy way to do this is to create another UDF where you pass a string and check to see if any special characters show up. If not return TRUE otherwise return FALSE

String#IsAlphaNumeric

LET
  chs = FROM ch IN SPLIT(string, "") WHERE NOT(Char#IsLetter(ch)) AND NOT(Char#IsNumber(ch)) SELECT ch
IN LENGTH(chs) = 0

See it in action!


Untitled_App_71-2023-01-27T18_08_01.465Z-full.branch.zip (35.2 KB)

1 Like

Oh, this is really good way to do it!

That is perfect! Thank you so much!

1 Like