-1

Hi I'm making a "secure password" project for school and I need help to check if a user input has "!@$%&#" and if they have it tell them their password is secure.

This is a project for school Im stuck doing this :(

var password = document.getElementById('password');
var eMsg = document.getElementById('feedback');
var minLength = 6;

function checkPass() {
  if (password.value.length < minLength) {
    eMsg.innerHTML = 'Password must have' + minLength + 'characters or more';
  } else {
    eMsg.innerHTML = '';
  }
}
<html>
<head>
  <title> Password Secured </title>
  <link rel="stylesheet" href="style.css">
</head>
  <body>
    <h1 class="logo"> logo</h1>
    <br/>
    <br/>
    <p class="input">Please type your password: <br/> <input type="text" id="password" />
      <div id="feedback"></div>
    </p>
    <br/>
    <p class="answer"></p>
    <br/>
    <br/>
    <p class="tips"> <br/>Tips tips tips </p>
  </body>
</html>
George
  • 6,630
  • 2
  • 29
  • 36
  • But having those characters does not - arbitrarily - make their password secure. Also, and more constructively, you need to learn about [regular expressions](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions), make an attempt and then ask for help when, or if, your attempted solution doesn't work. If your school is making that suggestion, and you have the capacity do so, I'd strongly suggest reading about password entropy and offering improvements to the requirements. – David Thomas Mar 28 '17 at 14:52
  • @DavidThomas who cares. it's a school project, not a production ready enterprise solution – micah Mar 28 '17 at 14:54
  • 1
    @Micah: I care. That's why I commented. Admittedly it's a little pedantic, fussy or whatever, but it still felt worth pointing it out. – David Thomas Mar 28 '17 at 14:55
  • Hard to know where you are. A Regex would be the accepted way. Brute force would be loop through the characters in the password and test each one. ps ###### is not a secure password... – Tony Hopkinson Mar 28 '17 at 14:57

4 Answers4

3

You can test a string using this regular expression:

function isValid(str){
 return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}

Possible duplicate of javascript code to check special characters

Community
  • 1
  • 1
Giulio Bambini
  • 4,695
  • 4
  • 21
  • 36
1

Use a regular expression.

// Returns TRUE if the string contains one of these characters.
// Returns FALSE otherwise
/[!@$%&#]/.test(password)
micah
  • 7,596
  • 10
  • 49
  • 90
0

Use the regular Expression

var password = document.getElementById('password');
password.addEventListener('blur',checkPass);
var eMsg = document.getElementById('feedback');
var minLength = 6;

function checkPass () {

    if (password.value.length < minLength ) {

        eMsg.innerHTML = 'Password must have' + minLength + 'characters or more';

    } else if(/[!@$%&#]/.test(password.value)){
        

        eMsg.innerHTML = 'Your password is secure';

    }
    

}
<html>
<head>

<title>  Password Secured </title>

<link rel="stylesheet" href="style.css">

</head>
 

<body>

<h1 class="logo"> logo</h1>

<br></br>

<br></br>

 

<p class="input">Please type your password: <br></br> <input type="text" id="password"/> <div id="feedback"></div> </p>

<br></br>

<p class="answer"></p>

<br></br>

<br></br>

 

<p class="tips"> <br></br>Tips tips tips </p>

 

</body>
Sagar V
  • 12,158
  • 7
  • 41
  • 68
0

You are looking for a code using a regular expression. What you should do is the following:

document.getElementById("myInput").value.match(/[\!\\@\$\%\&\#]+/g)

and check if this matched anything, if it does, then you properly matched any of those characters.

SmashingQuasar
  • 411
  • 2
  • 13