Using $.get with jquery validation

Posted by Jimmy McCarthy on Stack Overflow See other posts from Stack Overflow or by Jimmy McCarthy
Published on 2010-03-12T15:19:13Z Indexed on 2010/03/12 15:37 UTC
Read the original article Hit count: 390

I'm trying to use the Jquery builtin validator on my page. The issue is that I have certain fields that only are required if the JobID (entered into another field) does not already exist in our database. I have a simple service which simply takes JobID and returns True or False based on whether the JobID exists, but I can't seem to get this information where I want it. Some sample code:

$("#dep_form").validate({
        rules: {
            JobID: {
                required: true,
                digits: true,
                minlength: 3
            },
            OrgName: {
                required: function(element) { //This field is required if the JobID is new.
                    return $("#jobinfo").html().length==15; }
            }
        },
        messages: {
            JobID: {
                required: "Enter a Job Number.",
                digits: "Only numbers are allowed in Job ID's.",
                minlength: "Job Number must be at least 3 digits"
            },
            OrgName: {
                required: "Select a Practice from the dropdown list."
            }
        },
        errorClass: "ui-state-error-input",
    errorLabelContainer: "#errorbox",
    errorElement: 'li',
    errorContainer: "#validation_errors",
    onfocusout: false,
    onkeyup: false,
    focusinvalid: false
};

Currently, I'm using a lazy method to validate (shown above). However, I now have access to a service using the URL:

var lookupurl = "/deposits/jobidvalidate/?q=" + $("#id_JobID").val() + "&t=" + new Date().getTime();

which is a page which will contain just the word True or False based on whether that given JobID exists. I've tried half a dozen different ways of setting variables and calling functions within functions and still cannot get a way to simply return the value of that page (which I've been trying to access with $.get() ) to my validator, so that required is set to true when the Job does not exist and false if the job already exists. Any suggestions? Thanks.

© Stack Overflow or respective owner

Related posts about jquery-validate

Related posts about jQuery