I want the function to return true if the user has entered 4 or 6 digits. And false in all other cases. Entered letters, more characters, etc.

validatePIN("1234") === true validatePIN("12345") === false validatePIN("a234") === false 

Function code:

 function validatePIN (pin) { //return true or false if (pin.length == 4) { return true; } else if (pin.length == 6) { return true; } return false; } 

How can I correctly organize the verification that there are no characters in the line except numbers from 1 to 9.

Regular expression solution:

 ^(\d{4}|\d{6})$ 

Ps if anyone knows show how to do without RegExp

  • The number 0 not allowed? - Vladimir Gamalyan
  • one
    At the same time, add a reason why regulars are undesirable. - Vladimir Gamalyan
  • one
    @Vladimir Gamalian I don’t read regular expressions so far. they cause trouble - spectre_it
  • four
    The fact that you want to accomplish this task without regular expressions means one of two things: 1) you do not know regular expressions and are afraid to use them or 2) you think that they are slow. On the second point, I will immediately calm you down - you will not write faster. On the first point - now you have a great opportunity to learn, because the task is very small and simple. - Alexey Ukolov
  • 2
    here's an explanation with you regex101.com/r/uU0nB8/1 - Vladimir Gamalyan

3 answers 3

Well, if without regulars, then this:

 function validatePIN(pin) { var i = pin.length; if (i != 4 && i != 6) { return false; } while (i--) { if (pin[i] < '0' || pin[i] > '9') { return false; } } return true; } console.log('should be true:'); console.log(validatePIN('1234')); console.log(validatePIN('0129')); console.log(validatePIN('123456')); console.log('should be false:'); console.log(validatePIN('12345')); console.log(validatePIN('a234')); console.log(validatePIN('123a')); console.log(validatePIN('-123')); console.log(validatePIN('1.23')); console.log(validatePIN('0x99')); console.log(validatePIN('0:29')); console.log(validatePIN('0/29')); 

  • 12 editions? So what? - user207618
  • @Other watchman syndrome lutes me) - Vladimir Gamalyan
  • A well-known diagnosis, I myself suffer, although 12 is powerful, you, my friend, would see a doctor, he will prescribe medical meetings on the watch ... - user207618
  • @Other is interesting, if you roll back to the first revision, and then immediately insert the last option into it, how will it be displayed, like 13 revisions? - Vladimir Gamalyan
  • Take a chance! Plus, get a medal for a rollback. - user207618

ASCII range

 function validatePIN(pin, valid = true) { l = pin.length; if(l != 4 && l != 6) return false; Array.from(pin).forEach((s) => { s = s.charCodeAt(0); if(s > 57 || s < 48) valid = false; }); return valid; } console.log(validatePIN("1234")); console.log(validatePIN("0123")); console.log(validatePIN("123456")); console.log(validatePIN("12345")); console.log(validatePIN("a234")); console.log(validatePIN("123a")); console.log(validatePIN("-123")); console.log(validatePIN("1.23")); console.log(validatePIN("0x99")); console.log(validatePIN("0:29")); 

    Or this option:

     function validatePIN(pin) { if (isNaN(pin) || pin == 'null' || !isInteger(+pin)) { return false; } else if (pin.length == 4 || pin.length == 6) { return true; } return false; } function isInteger(num) { return (num ^ 0) === num; } console.log(validatePIN("123454")); //=== true console.log(validatePIN("1234")); //=== true console.log(validatePIN("a234")); //=== false console.log(validatePIN("a23445")); //=== false console.log(validatePIN('null')); //=== false console.log(validatePIN('1.23')); //=== false 

    • For validatePIN("-123") will mistakenly true - Vladimir Gamalyan
    • @VladimirGamalian corrected and added additional checks. - Astor
    • Now for validatePIN("1.23") - Vladimir Gamalyan
    • Thanks for participating, but this code does not pass validatePIN (.234) - spectre_it
    • Corrected. Now everything passes)) - Astor