Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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 Jquery的表单验证未按预期运行_Javascript_Jquery_Validation - Fatal编程技术网

Javascript Jquery的表单验证未按预期运行

Javascript Jquery的表单验证未按预期运行,javascript,jquery,validation,Javascript,Jquery,Validation,我正在使用jQuery验证表单中的一些字段,似乎有一个字段特别有问题(#inputTel) 如果输入了不正确的格式,下面会弹出一条错误消息,这很好,但问题是一旦输入了正确的格式,消息就不会消失 这里有一个完整的演示 这是相关章节: //电话验证 函数是有效的{ $this=$(“#inputTel”); var pattern=newregexp(“^\d{11}$”); if(pattern.test($this.val()){//valid if($this.closest(“.contro

我正在使用jQuery验证表单中的一些字段,似乎有一个字段特别有问题(
#inputTel

如果输入了不正确的格式,下面会弹出一条错误消息,这很好,但问题是一旦输入了正确的格式,消息就不会消失

这里有一个完整的演示

这是相关章节:

//电话验证
函数是有效的{
$this=$(“#inputTel”);
var pattern=newregexp(“^\d{11}$”);
if(pattern.test($this.val()){//valid
if($this.closest(“.control group”).hasClass(“error”))$this.closest(“.control group”).removeClass(“error”);
$this.sides(“.help inline”).css(“显示”、“无”);
返回true;
}else{//错误
if(!$this.nexist(“.control group”).hasClass(“error”)$this.nexist(“.control group”).addClass(“error”);
$this.sides(“.help inline”).css(“显示”、“块”);
返回false;
}
}
除此之外,其他所有字段均按预期工作。我的jQuery技能有限,因此我不确定如何解决此问题。

代码中的问题:

替换
var pattern=newregexp(“^\d{11}$”)带有
var模式=新的RegExp(/^\d{11}$/)

更新代码

//Tel Validate
function is_valid_tel() {
    $this = $("#inputTel");
    var pattern = new RegExp(/^\d{11}$/);// Update this line
    if (pattern.test($this.val())) { // valid
        if ($this.closest(".control-group").hasClass("error")) $this.closest(".control-group").removeClass("error");
        $this.siblings(".help-inline").css("display", "none");
        return true;
    } else { // error
        if (!$this.closest(".control-group").hasClass("error")) $this.closest(".control-group").addClass("error");
        $this.siblings(".help-inline").css("display", "block");
        return false;
    }
}

检查小提琴

您也可以使用以下类似的工具,减少立方体:

$(function() {
            function validateTheForm() {
              var ok = true;
                $(".input-medium").each(function() {
                if($(this).val() == "") //use regex actually here
                { 
                  $(this).next().css("display","inline");
                  ok = false;
                }
                else {
                    $(this).next().css("display","none");
                }


            });
                return ok;
            }
            $(".btn").click(function() {
              $("form").submit(validateTheForm());
              $("form").submit();

            });

        });

我强烈建议使用jQuery。我认为正则表达式有问题,当我把一个10位数的东西放到错误部分时,它也会转到错误部分而不是jQuery,是你的正则表达式弄乱了东西,还有对正则表达式本身的修改,我注意到它已经从它的引号中删除了,为什么需要删除“”呢?