使用密码验证的JavaScript和HTML表单

huangapple go评论72阅读模式
英文:

with password validification javascript and html form

问题

我正在尝试编写一个简单的任务。
我需要从HTML表单中获取密码,并检查是否包含数字和字母,并且密码不超过10个字母或不少于4个。
所以我写了一些(可能很糟糕)并且它不起作用(出奇不意 xD)。
无论如何,这是我的代码。

function checkpassword(checked_id, error_id, err_color){
     
     var upperCaseLetters = /[A-Z]/g;
     var lowerCaseLetters = /[a-z]/g;
     var numbers = /[0-9]/g;
     element_checked = document.getElementById(checked_id);
     element_error = document.getElementById(error_id);
     element_error.style.color = err_color;
     if (!checked_id.value.match(upperCaseLetters) && !checked_id.value.match(lowerCaseLetters)) {
         element_checked.style.backgroundColor = err_color;
         element_error.innerHTML = "请输入字母";
         return false;
     }
     else if(!checked_id.value.match(numbers))
     {
         element_checked.style.backgroundColor = err_color;
         element_error.innerHTML = "请输入数字";
         return false;
     }
     return true;
     
}
英文:

Im trying to code a simple task.
i need to get a password from html form, and check if there is both numbers and letters there, and that the password is not longer then 10 letters or short then 4.
so i wrote something (probably terrible) and its not working (surprisingly xD).
anyways, heres my code.

        function checkpassword(checked_id, error_id, err_color){
             
             var upperCaseLetters = /[A-Z]/g;
             var lowerCaseLetters = /[a-z]/g;
             var numbers = /[0-9]/g;
             element_checked = document.getElementById(checked_id);
             element_error = document.getElementById(error_id);
             element_error.style.color = err_color;
             if (!checked_id.value.match(upperCaseLetters) && !checked_id.value.match(lowerCaseLetters)) {
                 element_checked.style.backgroundColor = err_color;
                 element_error.innerHTML = "enter letters";
                 return false;
             }
             else if(!checked_id.value.match(numbers))
             {
                 element_checked.style.backgroundColor = err_color;
                 element_error.innerHTML = "Enter numbers";
                 return false;
             }
             return true;
             
       }

答案1

得分: 1

主要问题是您使用了 checked_id.value,应该是 element_checked.value

此外,一般建议使用 let 创建变量,而不是 var,如果您不需要它们成为全局变量。

英文:

The primary issue is that you used checked_id.value, which should be element_checked.value.

Also, generally it's advisable to create variables with let instead of var if you don't need them to be global.

huangapple
  • 本文由 发表于 2020年1月3日 20:12:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定