Skip to main content
udayasain741
April 7, 2023
Solved

Validate a field which accept 0,1,2,3,4,5,6

  • April 7, 2023
  • 5 replies
  • 0 views

HI Team,

I stuck on feild validation which will accept only below combination:

it can be 

0,1,2,3,4,5,6

or 0,1 or 1,2 combinations with all numbers or it can be only value which is less than 6 

I am able to achieve with below code

a!isNullOrEmpty(
stripwith(
ri!GBL_AutoCaseRunTimeConfig.config_value,
"0123456,"
)
)

but 

if user enters like 1,2,,,,,,, or 1,2,25 this is accepting , because it is under stripwith value i have added,  how can we validate ?

might be to use split with comma and use contains functions and also how we can remove comma at last ?

Any good suggestions ?

    Best answer by mikes0011

    Are you saying the user enters a text value of like "0,1" or "0,1,2", ... "0,1,2,3,4,5,6", and those permutations?  It's a little unclear what the delineation is here between valid/expected inputs and invalid ones.

    Also: is it order independent?  Accepts multiple values? Etc?  You didn't specify, but if these are constraints we aren't worried about, then the following might work:

    a!localVariables(
      local!input,
      
      local!validSet: {1, 2, 3, 4, 5, 6, 0},
      
      a!textField(
        value: local!input,
        saveinto: local!input,
        
        validations: {
          if(
            and(
              a!isNotNullOrEmpty(local!input),
              or(
                a!forEach(
                  split(local!input, ","),
                  or(
                    fv!item <> tostring(tointeger(fv!item)),
                    not(contains(local!validSet, tointeger(fv!item)))
                  )
                )
              )
            ),
            "Invalid Entry",
            {}
          )
        }
      )
    )

    Valid:

    Invalid:

    5 replies

    mikes0011
    mikes0011Answer
    Brainy
    April 7, 2023

    Are you saying the user enters a text value of like "0,1" or "0,1,2", ... "0,1,2,3,4,5,6", and those permutations?  It's a little unclear what the delineation is here between valid/expected inputs and invalid ones.

    Also: is it order independent?  Accepts multiple values? Etc?  You didn't specify, but if these are constraints we aren't worried about, then the following might work:

    a!localVariables(
      local!input,
      
      local!validSet: {1, 2, 3, 4, 5, 6, 0},
      
      a!textField(
        value: local!input,
        saveinto: local!input,
        
        validations: {
          if(
            and(
              a!isNotNullOrEmpty(local!input),
              or(
                a!forEach(
                  split(local!input, ","),
                  or(
                    fv!item <> tostring(tointeger(fv!item)),
                    not(contains(local!validSet, tointeger(fv!item)))
                  )
                )
              )
            ),
            "Invalid Entry",
            {}
          )
        }
      )
    )

    Valid:

    Invalid:

    The Expression Guru
    udayasain741
    April 10, 2023

    Yes Mike , You answered my question , Thank you, Only thing is if user enter duplicates like 1,2,3,3 we need to validate, But i handled this senario , Thanks for the help and you always rock

    mikes0011
    Brainy
    April 10, 2023

    Cool, thanks for confirming - if you get a chance, it'd be nice for you to hit the "verify" button [emoticon:0f36700dfeb3494fa10d47dd9e1f90a1]

    The Expression Guru