Are you first timer at Why not join us and start exchange your programming knowledge with everyone. Register today!

JS - Login condition example

0 votes
added Jul 6 in Javascript by LC Marshal Lieutenant (14,570 points)
edited Jul 12 by LC Marshal

If username as "admin", then prompt a password, if the input is empty, “Canceled.”, if it’s another string – then show “I don’t know you”.

The password is checked as follows:

  • If it equals “sacredpass”, then show “Welcome!”,
  • Another string – show “Wrong password”,
  • For an empty string or cancelled input, show “Canceled.”
let loginName = prompt('your login name', '');
if (loginName == 'admin') {
  let password = prompt('password','') 
    if (password == 'sacredpass') {
    } else if (password == '' || password == null) {
    } else {
      alert('Wrong password')
} else if (loginName == '') {
else {
  alert('Dont know you')

Please log in or register to response this reference. - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.