Archie
Archie

Reputation: 86

Simple PIN validation

Task: ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits. If the function is passed a valid PIN string, return true, else return false.

My solution:

function validatePIN (pin) {
  //return true or false
  if (!isNaN(pin) && Number.isInteger(pin) && pin.toString().length == 4 || pin.toString().length == 6) { 
    return true 
  } else { 
    return false 
  }
}

The only bug I get is when I pass 4 digits as a string ("1234") - it equals false.

Upvotes: 5

Views: 19058

Answers (8)

Nataliia Myronenko
Nataliia Myronenko

Reputation: 11

public static boolean validatePin(String pin) {
  return pin.matches("\\d{4}|\\d{6}");
}

Upvotes: 0

b00t
b00t

Reputation: 419

function validatePIN (pin) {
  return typeof pin === 'string' && // verify that the pin is a string
    Number.isInteger(+pin) && // make sure that the string is an integer when converted into a number
    [4, 6].includes(pin.length) // only accepts 4 and 6 character pins
}

Upvotes: 6

bazylevnik0
bazylevnik0

Reputation: 100

function validatePIN (pin) {
  if (pin.length == 4 || pin.length == 6) 
    {
      for (let i = 0; i < pin.length; i++)
        {
          if (pin[i] == "0" ||
              pin[i] == "1" ||
              pin[i] == "2" ||
              pin[i] == "3" ||
              pin[i] == "4" ||
              pin[i] == "5" ||
              pin[i] == "6" ||
              pin[i] == "7" ||
              pin[i] == "8" ||
              pin[i] == "9") ;
          else return false;
        }
      return true;
    }
  else return false;
}

this code checks the PIN-length and passes all test tasks with numbers and not numbers...

Upvotes: 0

Here is another way to solve using regular expression.

function validatePIN(pin) {
  return /^(\d{4}|\d{6})$/.test(pin)
}

validatePIN('2345')
//returns true

validatePIN('2.45')
//reutrns false

Upvotes: 0

David SUDI
David SUDI

Reputation: 1

function validatePIN (pin) {
    if (pin.length !== 4 && pin.length !== 6) {
    return false;
  }
  for (let i = 0; i < pin.length; i++) {
    if (pin[i] > '9' || pin[i] < '0') {
       return false;
    }

  }
  return true;
}

Upvotes: 0

Richard
Richard

Reputation: 1

function validatePIN (pin) {
  //return true or false
  return /^\d+$/.test(pin) && (pin.length === 4 || pin.length === 6)
}

Upvotes: 0

MichaelMaz
MichaelMaz

Reputation: 11

function validatePIN(pin) {
var isNumber = /^\d+$/.test(pin) && (pin.length == 4 || pin.length == 6)
return isNumber
}

validatePIN('0193')

//returns true

Upvotes: 1

guest271314
guest271314

Reputation: 1

You can use Array.prototype.every(), Array.prototype.some(), String.prototype.match()

<input type="text" />
<button>validate pin</button>
<script>
  var validatePIN = (args) => {[...args] = args;
 return args.every(v => v.match(/\d/)) &&
    [4, 6].some(n => args.length === n)};

  document.querySelector("button")
  .addEventListener("click", (e) =>
    alert(validatePIN(e.target.previousElementSibling.value))
  )
</script>

Upvotes: 0

Related Questions