简体   繁体   中英

how to validate datalist field by using jquery validator in php

Hi I am working as web application developer, I am stuck in the jquery validation. In my form have to show list employee by using tag in my form, if the client may be enter the correct name or enter few char and select the employee name from datalist. Suppose the client enter any name (except exist employee name from list), have to validate.

my html code:

<input list="pAdminID" id="pAdminName" name="pAdminName" onchange="GetID()" value="<?php echo $pAdminName; ?>">
<datalist name="pAdminID" id="pAdminID">
<?php foreach($emp as $row) { ?>
<option id="<?php echo $row['emp_id']; ?>" value="<?php echo $row['emp_name']; ?>"><?php } ?>
</datalist>
<input type="hidden" id="empID" name="empID">

my script : to get employee id

function GetID() {  
    var x = $('#pAdminName').val();
    var z = $('#pAdminID');
    var val = $(z).find('option[value="' + x + '"]');
    var empID = document.getElementById('empID');
    empID.value  = val.attr('id');
}

to validation:

$(function() {

    $("#project").validate({

    rules: {
        pName: "required",
        pAdminName: "required"
    },     

    messages: {
        pName: "Please enter the Project Name",
        pAdminName: "Please enter or select the Assignment name"
    }
});
});

please help how to validate the pAdminName field?

Try doing this:

jQuery.validator.addMethod("adminName", function(value, element) {
//process/validate here

    return value;);
}, "Must admin name");

And then applying this like so:

$('#project').validate({
    rules : {
        pAdminName: { adminName: true }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM