douji1853 2016-05-24 06:38
浏览 33

具有相同类的动态生成文本框的远程验证

I have an issue on my dynamic generated text box. I want required and remote validation for all text box.

HTML

<li>
    <label class="label">name 1</label>
    <input type="text" name="name_1" id="name_1" class="req_aap" value="" />
    <label id="aap_error" class="error"></label>
</li>
<li>
    <label class="label">name 2</label>
    <input type="text" name="name_2" id="name_2" class="req_aap" value="" />
    <label id="aap_error" class="error"></label>
</li>
<li>
    <label class="label">name 3</label>
    <input type="text" name="name_3" id="name_3" class="req_aap" value="" />
    <label id="aap_error" class="error"></label>
</li>
<li>
    <label class="label">name 4</label>
    <input type="text" name="name_4" id="name_4" class="req_aap" value="" />
    <label id="aap_error" class="error"></label>
</li>
................can be any number of fields................

JS

$.validator.addClassRules("req_aap", {
    required:true,
    remote:{
        url: "some_ajax.php",
        type: "post",
        data: {
            name: function() {return $(this).val();},
            tblNM:'someTable'
        },
        complete: function(data){
            return data;
        }
    }
});

some_ajax.php:

if(isset($_REQUEST['name'])) {
    if(getValFromTbl('name',$_REQUEST['tblNM'],'name="'.addContent($_REQUEST['name']).'"'.$fld)!='')
    {
        $valid = 'false';
    }
    else {
        $valid = 'true'; // Allowed
    }
    echo $valid;
}

getValFromTbl is the function which return record already exist or not. This works fine.

I got true and false exactly as i want but then also my jQuery validation continually gives error. It always go to false condition and gives validation error if I enter true value then also it gives error.

  • 写回答

2条回答 默认 最新

  • douyan1882 2016-05-26 20:28
    关注

    See if re-writing like this your some_ajax.php work for you.

    $valid = 'true'; // Set allowed by default
    if(isset($_REQUEST['name'])) {
    $valFromTbl = getValFromTbl('name',$_REQUEST['tblNM'],'name="'.addContent($_REQUEST['name']).'"'.$fld);  
        if(!empty($valFromTbl)) // found value in table - change validity
        {
        $valid = 'false'; // Not Allowed
        }
        echo $valid;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办