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

Parents
  • 0
    Certified Lead Developer

    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

  • 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.    

  • 0
    A Score Level 1
    in reply to ST07

    included a null check also for this , 

    a!dropdownField(
    choiceLabels: {
    "jan,feb,march"
    },
    choiceValues: {
    "jan,feb,march"
    },
    value: if(isnull(local!month),{},local!month),

    i am not sure how this thing works, initially year and month variables will be null only , user will enter the values.

  • +1
    Certified Lead Developer
    in reply to ST07

    When you have a dropdown field whose value will initially be null (so, most of the time), you need to also set a "placeholder label" value -- this is usually just text that will help the user know what to do, and shows up when the value is NULL, but doesn't count as a valid selection if the field is required.  Setting this correctly will cause your error message to go away.

    e.g.

    a!localVariables(
      local!year,
      a!dropdownField(
        choiceLabels: {
          "2020",
          "2021",
          "2022"
        },
        choiceValues: {
          "2020",
          "2021",
          "2022"
        },
        value: local!year,
        saveInto: local!year,
        placeholderLabel: "--Select Year--"
      )
    )

    (also, i fixed the choiceLabels and choiceValues - these should be arrays, the way you had them they were just strings that would cause them to only show up as one option instead of three)

  • 0
    A Score Level 1
    in reply to Mike Schmitt

    Thanks mike , both the approach works fine.  I had this last doubt , like in documentation how is text(38353, "mmm/dd/yyyy")   returns "Jan/03/2140"(Text)     . Actually i thought of using text function to convert input value to date type string. How this date is being calculated?

  • 0
    Certified Lead Developer
    in reply to ST07

    In general for dates, the text function is really only useful if the value you're starting with is an actual date value - i.e. "text(now(), "mmm/yyyy")" would work well, but i don't know if it would work very well with inputs that aren't a date value or something corresponding to this.

  • 0
    Appian Employee
    in reply to ST07

    There's two things going on here. First is the representation of a date value as a number. If you ever try to convert a date to an integer, the number represents the number of days before or after 1 January 2035 (see the todate() function docs for more information). So in the case you gave above, 38353 is considered that many days after 1 Jan 2035, which results in 3 Jan 2140.

    The second thing to consider is the method Appian uses to display dates using the text function. The text function can display many kinds of date formats depending on what format is provided as the second parameter. For example, the following options all display some kind of month:

    • "m" --> returns the month number without leading zeros; for example, January would be 1 and November would be 11
    • "mm" --> returns the month number with leading zeroes; for example, January would be 01 and November would be 11
    • "mmm" --> returns the three letter name of a month; for example January would be Jan and November would be Nov
    • "mmmm" --> returns the full month name

    All that being said, this function is used to go from a number to a string, but I believe you want the opposite (go from the text month name to a number). The best way to do this is to use choiceValues to save the number for month instead of the text name like this:

    a!localVariables(
      local!month,
      a!dropdownField(
        choiceLabels: {
          "Jan",
          "Feb",
          "Mar"
        },
        choiceValues: {
          "01",
          "02",
          "03"
        },
        value: local!month,
        saveInto: local!month,
        placeholderLabel: "--Select Month--"
      )
    )

  • 0
    A Score Level 1
    in reply to Peter Lewis

    ohh , thanks for the detailed explanation . So , i think as of now , we dont have any fuction to validate input string as a date format. We have to make customized validation check for that or using regex functions.

Reply Children
No Data