Validation for 4 digit pin

Hi,

 I have requirement to validate 4-digit pin. Below are the requirement of the 4-digit number

1. pin should not allow sequential numbers

2.  pin should not allow same numbers

example: 1234 or 4321 or 0000, 1111 or 4444 or 2222 or 3333 numbers are not allowed as a card pin.

can anyone please help me how to write logic for the validation.

thanks in advance!

  Discussion posts and replies are publicly visible

Parents
  • Hi, 
    Here is a solution which covers all edge cases. (Some humble bragging)
    The expression rule 'isPinValid' provides an array as a result. 
    The first item in the result is a Boolean indicating if the Pin is valid, the second item in the result is the validation message

    a!localVariables(
      /* Checks if the pin is compliant to the rules: has 4 digits, no sequences (ascending or descending) and not the same 4 digits */
      /* Get the digits of the pin - ri!pin is in text format */
      local!allDigits: tostring(ri!pin), 
      local!count: len(local!allDigits),
      local!digits: a!forEach(items: enumerate(local!count),expression: tointeger(local!allDigits[fv!index])),
      /* Prep for the sequence test, calculating the difference between the digits - ldrop is for the edge case if the pin starts with a 1 */
      local!seq:ldrop({0,local!digits} - {local!digits,0},1),
      /* Same digits? */
      local!sd: if(1=length(union(local!digits,local!digits)),true,false),
      /* Check if we have a sequence of numbers e. g. 2345 or 8765 */
      local!seq1: if(3<=length(wherecontains(-1,local!seq)),true,false),
      local!seq2: if(3<=length(wherecontains(1,local!seq)),true,false),
      /* Is the pin valid (true), or is one of the rules violated? (false) */
      local!check: not(or(4<>local!count,local!sd,local!seq1,local!seq2)),
       { 
        local!check,
        /* Providing the confirmation and/or error message  */
        a!match(value:true,
        whenTrue: count(local!digits)<>length(local!digits),
        then: "Pin can only contain Digits",
        whenTrue: local!check,
        then: "Pin is valid",
        whentrue:4<>local!count,
        then: "Pin has not 4 digits",
        whentrue: local!sd,
        then: "Pin can't have the same 4 digits",
        whenTrue: or(local!seq1,local!seq2),
        then: "Pin can't have digits in " & if(local!seq1,"a","de") & "cending sequence",
        default: "Not a valid pin"
        )
      }
    )

Reply
  • Hi, 
    Here is a solution which covers all edge cases. (Some humble bragging)
    The expression rule 'isPinValid' provides an array as a result. 
    The first item in the result is a Boolean indicating if the Pin is valid, the second item in the result is the validation message

    a!localVariables(
      /* Checks if the pin is compliant to the rules: has 4 digits, no sequences (ascending or descending) and not the same 4 digits */
      /* Get the digits of the pin - ri!pin is in text format */
      local!allDigits: tostring(ri!pin), 
      local!count: len(local!allDigits),
      local!digits: a!forEach(items: enumerate(local!count),expression: tointeger(local!allDigits[fv!index])),
      /* Prep for the sequence test, calculating the difference between the digits - ldrop is for the edge case if the pin starts with a 1 */
      local!seq:ldrop({0,local!digits} - {local!digits,0},1),
      /* Same digits? */
      local!sd: if(1=length(union(local!digits,local!digits)),true,false),
      /* Check if we have a sequence of numbers e. g. 2345 or 8765 */
      local!seq1: if(3<=length(wherecontains(-1,local!seq)),true,false),
      local!seq2: if(3<=length(wherecontains(1,local!seq)),true,false),
      /* Is the pin valid (true), or is one of the rules violated? (false) */
      local!check: not(or(4<>local!count,local!sd,local!seq1,local!seq2)),
       { 
        local!check,
        /* Providing the confirmation and/or error message  */
        a!match(value:true,
        whenTrue: count(local!digits)<>length(local!digits),
        then: "Pin can only contain Digits",
        whenTrue: local!check,
        then: "Pin is valid",
        whentrue:4<>local!count,
        then: "Pin has not 4 digits",
        whentrue: local!sd,
        then: "Pin can't have the same 4 digits",
        whenTrue: or(local!seq1,local!seq2),
        then: "Pin can't have digits in " & if(local!seq1,"a","de") & "cending sequence",
        default: "Not a valid pin"
        )
      }
    )

Children
No Data