Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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.serialize()中的每个参数?_Jquery_Validation_Serialization - Fatal编程技术网

如何验证jquery.serialize()中的每个参数?

如何验证jquery.serialize()中的每个参数?,jquery,validation,serialization,Jquery,Validation,Serialization,我有一个如下的表格: <form id="gb"> <div><input type="text" name="a" value="" /></div> <div><input type="text" name="b" value="" /></div> <div><input type="submit" name="s" value="Submit" /></div>

我有一个如下的表格:

<form id="gb">
  <div><input type="text" name="a" value="" /></div>
  <div><input type="text" name="b" value="" /></div>
<div><input type="submit" name="s" value="Submit" /></div>
</form>
我想在ajax操作之前验证a&b值,如何做到这一点?

您可以使用

.serializeArray()
方法创建对象的JavaScript数组, 准备好编码为JSON字符串。它对jQuery对象进行操作 表示一组表单元素


始终执行服务器端验证。也就是说,如果您想在进行服务器端验证之前添加客户端验证,请签出这个jquery验证插件。

Javascript HTML

带有提交验证和默认消息的简单注释表单

名称
*

电子邮件 *

统一资源定位地址

你的评论 *


为什么不先验证,然后如果a&b通过,则序列化。@VIDesignz:mm。。我试试看。Thanksi已被读取。serializeArray(),但我仍然不知道如何使用它,特别是在验证某个参数值中的NULL或notnull时​​.呵呵。。我已经试过了,但是在使用validate函数时,ajax提交是有效的。如何解决这个问题?这是我的小提琴:太棒了。。就像我想要的。。谢谢,不需要任何插件就可以进行验证。我已经有很多js插件了
$('form#gb').submit(function() {
  $catch = $(this).serialize();

  /* This produces a query string: 
  a=XXXX&b=XXXXX */

  ...
  /* i want to validate a & b here */
  ...

  $.ajax({
     /* My Ajax Code */
  });

});
$(document).ready(function(){
    $("#commentForm").validate({submitHandler: function(form) {
        // some other code
        // maybe disabling submit button
        // then:
        $.ajax({
             /* My Ajax Code */
        });
    });
});
<form class="cmxform" id="commentForm" method="get" action="">
    <fieldset>
         <legend>A simple comment form with submit validation and default messages</legend>
    <p>
    <label for="cname">Name</label>
    <em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
    </p>
    <p>
    <label for="cemail">E-Mail</label>
    <em>*</em><input id="cemail" name="email" size="25"  class="required email" />
    </p>
    <p>
    <label for="curl">URL</label>
    <em>  </em><input id="curl" name="url" size="25"  class="url" value="" />
    </p>
    <p>
    <label for="ccomment">Your comment</label>
    <em>*</em><textarea id="ccomment" name="comment" cols="22"  class="required"></textarea>
    </p>
    <p>
    <input class="submit" type="submit" value="Submit"/>
    </p>
    </fieldset>
</form>