Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RegExp可以完美地使用.html扩展名,但不能使用.php扩展名_Javascript_Php_Html - Fatal编程技术网

Javascript RegExp可以完美地使用.html扩展名,但不能使用.php扩展名

Javascript RegExp可以完美地使用.html扩展名,但不能使用.php扩展名,javascript,php,html,Javascript,Php,Html,我用JavaScript编写了一个脚本,当我尝试在扩展名为.html的页面上运行它时,它工作得非常好。但是,当我尝试在带有.php的页面上运行它时,会出现以下错误: Uncaught TypeError: Cannot read property 'match' of undefined at HTMLButtonElement.CheckStrength 未捕获的TypeError:无法读取未定义的属性“match” 在HTMLButtonElement.CheckStrength处 这是我的

我用JavaScript编写了一个脚本,当我尝试在扩展名为.html的页面上运行它时,它工作得非常好。但是,当我尝试在带有.php的页面上运行它时,会出现以下错误:

Uncaught TypeError: Cannot read property 'match' of undefined at HTMLButtonElement.CheckStrength 未捕获的TypeError:无法读取未定义的属性“match” 在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.");
        }
}
var x=document.getElementById(“密码”);
var按钮=document.getElementById(“提交”);
变量检查=/[0-9]+$/;
函数CheckStrength(){
如果(x.value.length>7&&x.value.match(检查)!=null)
{
警报(“您的密码很强。”);
}

否则,如果(x.value.length>3&&x.value.length您的代码在.php扩展中运行良好,我只能在目标元素不存在或在元素之前插入javascript时重现该错误。请检查这些

  • 检查是否有打字错误
  • 将脚本标记放在body元素的结束标记之前(以确保脚本执行时目标元素可用)
  • 将javascript变量定义包装到

扩展没有什么区别,除了,我猜问题在于DOM中没有元素
x
。或者,在呈现元素
x
之前运行此代码(例如,如果脚本在HTML之前).Hi Andaar,这与您的问题无关,但可能会使其完全无效。我建议您考虑使用ZXCVN进行密码强度检查。@cale_b我知道我以前和(在我的情况下)无法运行此代码。这是我的php页面:


Check@Geoff我将在学校进行考试,我不能使用外部库或脚本,但感谢您的建议。我知道这一点(您在主题标题中有此信息),但我想在.php文件中运行它。@Andaar我已经在一个.php文件中对它进行了测试,除非“#password”未定义,否则它可以正常工作。@Andaar很高兴知道。很高兴与大家分享导致错误的原因以及如何修复错误。我阅读了您编写的第一点和最后一点,当然中间的一点就是答案;)