英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论