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

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.

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;
}

I tried the exact same example within form and as I didn't have the getValFromTbl(), I tried with writing true in place of that as well as checking the name string. Worked fine.. See if the code helps..

html page

<!DOCTYPE html>
<html>
<head>
    <title>trying jquery validation</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $('.frm').validate();
            $.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;
                    }
                }
            });
        });
    </script>
</head>
<body>

<form class="frm">
    <ul>
        <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>
    </ul>
    <input type="submit"></input>
</form>

</body>
</html>

some_ajax.php

<?php
if(isset($_REQUEST['name'])) {
    if($_REQUEST['name'] != '')
    {
        $valid = 'false';
    }
    else {
        $valid = 'true'; // Allowed
    }
    echo $valid;
}