【发布时间】:2017-01-21 22:50:28
【问题描述】:
我用 JavaScript 编写了一个脚本,当我尝试在扩展名为 .html 的页面上运行它时,它运行良好。但是当我尝试在带有 .php 的页面上运行它时,出现以下错误:
未捕获的类型错误:无法读取未定义的属性“匹配” 在 HTMLButtonElement.CheckStrength
这是我的代码:
var x = document.getElementById("password");
var button = document.getElementById("submit");
var check = /[0-9]+$/;
function CheckStrength(){
if(x.value.length > 7 && x.value.match(check) != null)
{
alert("Your password is strong.");
}
else if (x.value.length > 3 && x.value.length <= 7)
{
alert("Your password strength is medium.");
}
else
{
alert("Your passwword s**k.");
}
}
【问题讨论】:
-
扩展名没有任何区别,除了,我猜问题是DOM中没有元素
x。或者,您在呈现元素x之前运行此代码(例如,如果脚本在 HTML 之前)。 -
嗨,Andaar,这与您的问题无关,但可能会完全无效。我建议您考虑使用 zxcvbn 进行密码强度检查。
-
@cale_b 我知道我无法在 和
-
@Geoff 我将在学校参加考试,我无法使用外部库或脚本,但感谢您的建议。
标签: javascript php html