小编use*_*153的帖子

如何使用Codeigniter和Jquery验证多维数组

嗨,我需要验证这样的多维形式

<input type="text" class="input-xlarge span5 req" id="contact_first_name" name="hotel[<?=$id?>][contact_first_name]" value="<?= set_value('hotel[contact_first_name]') ?>">
<input type="text" class="input-xlarge span5 req" id="contact_last_name" name="hotel[<?=$id?>][contact_last_name]" value="<?= set_value('hotel[contact_last_name]') ?>">
Run Code Online (Sandbox Code Playgroud)

我不知道最终数组的维度,因为输入是通过jquery动态添加的.

我在服务器端使用Codeigniter Form_Validation,在客户端使用JQuery Validator通过JQuery.

这是我的form_validation规则

$config['add_hotel'] = array(
array(
    'field' => 'hotel[][hotel_name]', 
    'label' => 'Hotel Name', 
    'rules' => 'required'
    ),    
array(
    'field' => 'hotel[][contact_first_name]', 
    'label' => 'First Name', 
    'rules' => 'trim|required'
    ),
array(
    'field' => 'hotel[][contact_last_name]', 
    'label' => 'Last Name', 
    'rules' => 'trim|required'
    ),
Run Code Online (Sandbox Code Playgroud)

这就是我通过jquery验证器做的方式

$("#add_hotel").validate({
rules: {
    "hotel[][hotel_name]": "required"

  /*  errorElement: "div",
    wrapper: "div"*/
},
messages: {
   "hotel[][hotel_name]": …
Run Code Online (Sandbox Code Playgroud)

arrays validation jquery codeigniter multidimensional-array

7
推荐指数
1
解决办法
7648
查看次数