Kumar Shanmugam
Kumar Shanmugam

Reputation: 597

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?

Upvotes: 0

Views: 1298

Answers (1)

Drixson Ose&#241;a
Drixson Ose&#241;a

Reputation: 3641

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

Upvotes: 1

Related Questions