开发者问题收集

JavaScript 中特殊字符的密码验证

2022-05-23
2174

我正在编写一个密码验证代码,其中至少包含: 一个小写字母 一个大写字母 一个数字 一个特殊字符 最少 8 个字符,最多 12 个字符

但无法验证特殊字符,其余上述所有条件均有效

HTML 代码:

<label for="psw"><b>Password <i class="fa fa-asterisk" style="font-size: 12px; color: red"></i>:</b></label>
<input id="pswd" name="pswd" type="password" placeholder="Enter Password" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-])$" title="Must contain at least one Special Character and one Number and one Uppercase and Lowercase letter, and at least 8 or more characters" required>  <br>

                <div id="message">
                    <h3>Password must contain the following:</h3>
                    <p id="letter" class="invalid">A <b>lowercase</b> letter</p>
                    <p id="capital" class="invalid">A <b>capital (uppercase)</b>letter</p>
                    <p id="number" class="invalid">A <b>number</b></p>
                    <p id="length" class="invalid">Minimum <b>8 characters</b></p>
                    <p id="spcl" class="invalid">A <b>Special character</b></p>
                    <p id="maxlength" class="invalid">Minimum <b>8 character</b> and Maximum <b>12 character</b></p>

                </div>

Javascript

<script type="text/javascript">
         var myInput = document.getElementById("pswd");

         var letter = document.getElementById("letter");
         var capital = document.getElementById("capital");
         var number = document.getElementById("number");
         var maxlength = document.getElementById("maxlength");
         var spcl = document.getElementById("spcl");
// When the user clicks on the password field, show the message box
         myInput.onfocus = function () {
         document.getElementById("message").style.display = "block";
         }

         // When the user clicks outside of the password field, hide the message box
         myInput.onblur = function () {
         document.getElementById("message").style.display = "none";
                            }

         // When the user starts to type something inside the password field
          myInput.onkeyup = function () {
                            // Validate lowercase letters
                            var lowerCaseLetters = /[a-z]/g;
                            if (myInput.value.match(lowerCaseLetters)) {
                                letter.classList.remove("invalid");
                                letter.classList.add("valid");
                            } else {
                                letter.classList.remove("valid");
                                letter.classList.add("invalid");
                            }

                            // Validate capital letters
                            var upperCaseLetters = /[A-Z]/g;
                            if (myInput.value.match(upperCaseLetters)) {
                                capital.classList.remove("invalid");
                                capital.classList.add("valid");
                            } else {
                                capital.classList.remove("valid");
                                capital.classList.add("invalid");
                            }

                            // Validate numbers
                            var numbers = /[0-9]/g;
                            console.log(numbers);
                            if (myInput.value.match(numbers)) {
                                number.classList.remove("invalid");
                                number.classList.add("valid");
                            } else {
                                number.classList.remove("valid");
                                number.classList.add("invalid");
                            }
                            
                            var spcl = /[!@#$%^&*_=+-]/g;
                            console.log(spcl);
                            if (myInput.value.match(spcl)) {
                                spcl.classList.remove("invalid");
                                spcl.classList.add("valid");
                            } else {
                                console.log(myInput.value);
                                spcl.classList.remove("valid");
                                spcl.classList.add("invalid");
                            }


                            //Validate max length
                            if (myInput.value.length >= 8 && myInput.value.length <= 12) {
                                console.log('in min max condition');
                                console.log(myInput.value.length);
                                maxlength.classList.remove("invalid");
                                maxlength.classList.add("valid");
                            } else {
                                maxlength.classList.add("invalid");
                                maxlength.classList.remove("valid");
                            }
                        }
                    </script>

当我检查特殊字符时

var spcl = /[!@#$%^&*_=+-]/g;
                        console.log(spcl);
                        if (myInput.value.match(spcl)) {
                            spcl.classList.remove("invalid");
                            spcl.classList.add("valid");
                        } else {
                            console.log(myInput.value);
                            spcl.classList.remove("valid");
                            spcl.classList.add("invalid");
                        }

在控制台中显示错误为:未捕获的类型错误:无法读取未定义的属性(读取“remove”)

在打印变量“spcl”的 classList 时,我得到的输出为“无效”,但仍然面临此未捕获的类型错误:无法读取未定义的属性(读取“remove”)

3个回答

检查这个问题: 检查字符串中的特殊字符

对于特殊字符,请使用以下方法:

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;

if(format.test(string)){
  return true;
} else {
  return false;
}
Amirhossein Hassani
2022-05-23

通过这个您可以检查字符串是否包含特殊字符。

//Regex for Valid Characters i.e. Alphabets, Numbers and Space.
        var regex = /^[A-Za-z0-9 ]+$/
 
        //Validate TextBox value against the Regex.
        var isValid = regex.test(document.getElementById("txtName").value);
        if (!isValid) {
            alert("Contains Special Characters.");
        } else {
            alert("Does not contain Special Characters.");
        }
Raj Parmar
2022-05-23

正如我所评论的,您将两个变量命名为 spcl。重命名后,它就可以正常工作,您的正则表达式没有任何问题。另外,我注意到您跳过了最小长度验证,因此我添加了它。

var myInput = document.getElementById("pswd");
var letter = document.getElementById("letter");
var capital = document.getElementById("capital");
var number = document.getElementById("number");
var maxlength = document.getElementById("maxlength");
var spcl = document.getElementById("spcl");
var length = document.getElementById("length");

// When the user clicks on the password field, show the message box
myInput.onfocus = function () {
   document.getElementById("message").style.display = "block";
  }

// When the user clicks outside of the password field, hide the message box
myInput.onblur = function () {
   document.getElementById("message").style.display = "none";
  }

// When the user starts to type something inside the password field
myInput.onkeyup = function () {
   // Validate lowercase letters
   var lowerCaseLetters = /[a-z]/g;
   if (myInput.value.match(lowerCaseLetters)) {
      letter.classList.remove("invalid");
      letter.classList.add("valid");
   } else {
      letter.classList.remove("valid");
      letter.classList.add("invalid");
   }

   // Validate capital letters
   var upperCaseLetters = /[A-Z]/g;
   if (myInput.value.match(upperCaseLetters)) {
      capital.classList.remove("invalid");
      capital.classList.add("valid");
   } else {
      capital.classList.remove("valid");
      capital.classList.add("invalid");
   }

   // Validate numbers
   var numbers = /[0-9]/g;
   console.log(numbers);
   if (myInput.value.match(numbers)) {
      number.classList.remove("invalid");
      number.classList.add("valid");
   } else {
      number.classList.remove("valid");
      number.classList.add("invalid");
   }
                            
   var special = /[!@#$%^&*_=+-]/g;
   console.log(special);
   if (myInput.value.match(special)) {
      spcl.classList.remove("invalid");
      spcl.classList.add("valid");
   } else {
      console.log(myInput.value);
      spcl.classList.remove("valid");
      spcl.classList.add("invalid");
   }


   //Validate max length
   if (myInput.value.length >= 8 && myInput.value.length <= 12) { 
      console.log('in min max condition');
      console.log(myInput.value.length);
      maxlength.classList.remove("invalid");
      maxlength.classList.add("valid");
  } else {
      maxlength.classList.add("invalid");
      maxlength.classList.remove("valid");
  }
  
  //Validate min length
   if (myInput.value.length >= 8) { 
      length.classList.remove("invalid");
      length.classList.add("valid");
  } else {
      length.classList.add("invalid");
      length.classList.remove("valid");
  }
}
.invalid {
color: red
}

.valid {
color: green
}
<label for="psw"><b>Password <i class="fa fa-asterisk" style="font-size: 12px; color: red"></i>:</b>
    </label>
    <input id="pswd" name="pswd" type="password" placeholder="Enter Password" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-])$" title="Must contain at least one Special Character and one Number and one Uppercase and Lowercase letter, and at least 8 or more characters" required><br>
    <div id="message">
       <h3>Password must contain the following:</h3>
       <p id="letter" class="invalid">A <b>lowercase</b> letter</p>
       <p id="capital" class="invalid">A <b>capital (uppercase)</b>letter</p>
       <p id="number" class="invalid">A <b>number</b></p>
       <p id="length" class="invalid">Minimum <b>8 characters</b></p>
       <p id="spcl" class="invalid">A <b>Special character</b></p>
       <p id="maxlength" class="invalid">Minimum <b>8 character</b> and Maximum <b>12 character</b>
       </p>
    </div>
nedoder
2022-05-23