Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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
Jquery 在提交时验证多个输入_Jquery - Fatal编程技术网

Jquery 在提交时验证多个输入

Jquery 在提交时验证多个输入,jquery,Jquery,我正在尝试验证多个输入,以确保它们在我提交时不为空。然后在#error中添加一些文本 我的表单如下所示: <div class="span12"> <form method="post" name="bdayInputFormMain" action="/bdayremind_maininput/" class="form-inline"> {% csrf_token %} <input name="n

我正在尝试验证多个输入,以确保它们在我提交时不为空。然后在#error中添加一些文本

我的表单如下所示:

<div class="span12">
        <form method="post" name="bdayInputFormMain" action="/bdayremind_maininput/" class="form-inline">
        {% csrf_token %}
                <input name="name" type="text" class="input-medium" placeholder="Name">
                <input name="dob" type="text" class="input-small datepicker" placeholder="Date of Birth">
                <input name="addr" type="text" class="input-xlarge" placeholder="Address">
                <button type="submit" class="btn">Update</button>
        </form>
</div>

{%csrf_令牌%}
更新

谢谢,

这个验证库看起来很不错,可以满足您的需要。

请查看文档以了解更多信息。编写自己的验证方法相当容易,也可以像其他人建议的那样使用库

$('form')。提交(函数(){
var$emptyFields=$();
$('form')。查找('input')。每个(
函数(){
if($(this).val()==“”){
$emptyFields.push(此)
}
});
如果($emptyFields.length!=0){
$('form').find('input').css(“border”,”);
$emptyFields.css(“边框”,“1px实心红色”);
$(“#error”).html(“这是我的错误消息”);
返回false;
}否则{
返回true;
}
});

是啊!jquery验证是客户机最好的验证程序之一。对于那些必填字段,您应该使用required类

见医生

您的表单如下所示:


我希望有帮助

效果很好。但是,如果我更改代码以滑入错误消息,如何滑下页面的其余部分?如果仍然卡住,请尝试一些方法,并提出新问题
$('form').submit(function(){
   var $emptyFields = $();
   $('form').find('input').each(
       function(){
              if($(this).val() == ""){
                   $emptyFields.push(this)
             }
        });
  if($emptyFields.length != 0){
    $('form').find('input').css("border","");
    $emptyFields.css("border", "1px solid red");
    $("#error").html("<b><i>This is my error message</i></b>");
    return false;
  }else{
    return true;
  }
});