I have input for a password and need a js function that will validate my password and return true or false to me. No I can not understand where you can start. The password must contain letters, must not begin with a digit, must not contain a space, and these characters "-", "(", ")", "/". Need to write in regular javascript.

Closed due to the fact that the essence of the question is incomprehensible to the participants by Igor , freim , Vadizar , aleksandr barakin , mkkik 25 Apr at 6:18 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What is the actual problem? - Vearo 2:12 pm
  • The function itself is a problem - Gor Margaryan
  • Do not forget to do the same on the server. - And
  • I’m not quite on the topic, but how do I get websites that make me hook up my password so that it fits into their wild patterns. What, are you going to keep this password in the mainframe? Where do all these restrictions come from? - Sergey Nudnov

1 answer 1

Use several regular expressions:

const beginWithoutDigit = /^\D.*$/ const withoutSpecialChars = /^[^-() /]*$/ const containsLetters = /^.*[a-zA-Z]+.*$/ let password = "12345abcde" if( beginWithoutDigit.test(password) && withoutSpecialChars.test(password) && containsLetters.test(password) ){ console.log('ok'); } else { console.log('wrong'); } 

With this approach, it is easy to add other conditions. For example:

 const minimum8Chars = /^.{8,}$/ const withoutSpaces = /^[\S]$/ ... 

 const password_field = document.getElementById("password_field") const message = document.getElementById("message") let password const beginNotDigit = /^\D.*$/ const withoutSpecialChars = /^[^-() ]*$/ const containсLetters = /^.*[a-zA-Z]+.*$/ function check(){ password = password_field.value message.innerHTML = "password \""+password+"\"" if( beginNotDigit.test(password) && withoutSpecialChars.test(password) && containсLetters.test(password) ){ message.innerHTML += " is allowed!" } else { message.innerHTML += " is not allowed!" } } 
 Password:<br> <input id="password_field" type="password"/><br> <button onclick="check()"> Click Me for Password</button><br> <div id="message"></div>