String validation

A Score Level 1

Hi , 

How can we apply string data validation in the textfield in Appian. I have a requirement in which the first two alpahbets, should  be either a or A and the second one will be c or C. In short for a particular string , how can i check the character value at a particular index and apply validation.

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Hi , you can use function charat(text,index) to perform this validation.

  • In addition to charat(), you might also check out the function left(); left("aces", 2) would result "ac", which should satisfy your use case.  I'll post a more expansive code sample here, just to be thorough:

    a!textField(
      label: "My Text",
      value: ri!textInput,
      saveInto: ri!textInput,
      validations: {
        if(
          upper(left(ri!textInput, 2)) = "AC",
          null(),
          "Text must begin with the string 'AC' (case insensitive)."
        )
      }
    )

  • 0
    A Score Level 1
    in reply to Mike Schmitt

    hi mike , sorry for late reply , what if i want that the input text (string) is of MM/YYYY format ,  i am using validation on text filed. I have to extract only month and year and save them into db in mm/yyyy format

  • Why not use an Appian Date field to capture a date type value, then extract the month and year from that?  This would keep you from having to use a lot of extra custom validation. 

    Doing this using text would be possible, but it would be frustrating and complicated.  Perhaps less complicated if you have the regex plugin and you're already pretty familiar with how to use regex.

  • 0
    Certified Lead Developer
    in reply to ST07

    You add to yourself far greater work of avoiding months like 99, 55, AJ, RJ, GG, QQ, $%, **, or years like 1527, 1066, 9999, 4597, or "Spider-Man 2099" or "Steve Jobs' Birthday".

    It's so much simpler to just use the component that's already designed to prevent users from inputting anything other than a date.

  • Could you also use dropdown fields to limit their options? If you provide a dropdown for month and year right next to each other, a validation wouldn't be necessary because you would only display months and years that are valid.

  • 0
    Certified Lead Developer
    in reply to ST07

    Date field or paired dropdowns work really well.  One of the key principals of good UX is to, where possible, make it extremely difficult for the end user to make a mistake.  

  • 0
    A Score Level 1
    in reply to Mike Schmitt

    i tried that , but the date field  calender thing should not be  visible to end user, so thats why im using the text field with validation ,

    a!textField(
    value: ri!monthEndCDT.monthendkey,
    saveInto: {
    ri!monthEndCDT.monthendkey
    },
    validations: if(
    and( like(
    ri!monthEndCDT.monthendkey,
    "[0-9][0-9]/[0-9][0-9][0-9][0-9]"
    ), len(ri!monthEndCDT.monthendkey<7)),
    "valid date",
    "enter valid date Value MM/YYYY"
    ),

    ),

    But this  is displaying error message in both cases, valid and invalid both. It works but for valid value also , it displays red errored out  textfield

  • 0
    Appian Employee
    in reply to ST07

    So when you set up a validation in Appian, the result of the validations parameter should either be some text (which is what will display as the error message) or null. If a text is returned, Appian assumes that a validation should be triggered, while null means no validation should show. If you want to use the expression above, you will probably need something like this:

    a!textField(
      value: ri!monthEndCDT.monthendkey,
      saveInto: {
        ri!monthEndCDT.monthendkey
      },
      validations: if(
        and(
          like(
            ri!monthEndCDT.monthendkey,
            "[0-9][0-9]/[0-9][0-9][0-9][0-9]"
          ),
          len(ri!monthEndCDT.monthendkey<7)
        ),
        null,
        "enter valid date Value MM/YYYY"
      )
    )

    That being said, I still wouldn't recommend using a text field for this - two dropdowns would probably work a lot better and be more clear for the user.

  • 0
    A Score Level 1
    in reply to Peter Lewis

    Hi , thanks it works when i replace that with null.  i tried using that with dropdwon also, but i got an error . its basically an editble grid . i need to save month end key in mm/yyyy format. for dropdown , i have made 2 dropdown for year and month. But when i click on add rows dynamic link , error is there.

    Interface Definition: Expression evaluation error at function a!forEach [line 60]: Error in a!forEach() expression during iteration 1: Expression evaluation error at function a!dropdownField [line 90]: A dropdown component [label=“”] has an invalid value for “value”. Value cannot be null.

    a!dropdownField(          ------------->>>> line 90
    choiceLabels: {
    "jan,feb,march"
    },
    choiceValues: {
    "jan,feb,march"
    },
    value: local!month,
    saveInto: local!month
    ),
    a!dropdownField(
    choiceLabels: {
    "2020,2021,2022"
    },
    choiceValues: {
    "2020,2021,2022"
    },
    value: local!year,
    saveInto: local!year
    ),

    my plan is to take both these local values , append them , include a "/" string in between and save in the ri!monthEndCDT.monthendkey.