Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

jQuery Validation Help Needed

I am having trouble with some extra validation using the jquery form plugin.  My goal is to run an ajax validation check on a valid form submit from the user and if invalid show a message and stop the form from being submitted.  Unfortunately currently it currently either checks and shows the warning but still submits the form or if I place another "return false;" it will stop the form even if it is valid.  The code shown below is the pre-submit function which is where I think the remote verification needs to go to have the correct behavior.  Any suggestions would be welcome!
 
function showRequest() {
                    if($("#visitorSignin").valid()){
                        $.ajax({
                            url: 'checkDeniedPersonsList.php',
                            data: 'lName='+$('#lName').val(),// get and send user input
                            success: function(data) {
                                if(data == 'true'){
                                    showWarningFlash("You are on the denied persons list please present your crendentials to the receptionist");
                                    return false; //a denied person
                                }else if(data == 'false'){
                                    if($("input[name='usCitizen']:checked").val()==1){
                                        var labelFullName = $('#fName').val()+" "+$('#lName').val();
                                        var labelCompany = $('#companyName').val();
                                        var labelCitizen = "true";
                                        var labelType = "contractor";
                                        printLabel(labelFullName,labelCompany,labelCitizen,labelType);
                                    }else if($("input[name='usCitizen']:checked").val()==0){
                                        var labelFullName = $('#fName').val()+" "+$('#lName').val();
                                        var labelCompany = $('#companyName').val();
                                        var labelCitizen = "false";
                                        var labelType = "contractor";
                                        printLabel(labelFullName,labelCompany,labelCitizen,labelType);
                                    }
                                } // valid person
                            } //end of success function
                        }) //end of ajax function
                    }else {
                        return false; // form invalid
                    }
                }

Open in new window

0
PhillipsPlastics
Asked:
PhillipsPlastics
  • 3
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
try this :


function showRequest() {
                    if($("#visitorSignin").valid()){
                        $.ajax({
async: false,
                            url: 'checkDeniedPersonsList.php',
                            data: 'lName='+$('#lName').val(),// get and send user input
                            success: function(data) {
                                if(data == 'true'){
                                    showWarningFlash("You are on the denied persons list please present your crendentials to the receptionist");
                                    return false; //a denied person
                                }else if(data == 'false'){
                                    if($("input[name='usCitizen']:checked").val()==1){
                                        var labelFullName = $('#fName').val()+" "+$('#lName').val();
                                        var labelCompany = $('#companyName').val();
                                        var labelCitizen = "true";
                                        var labelType = "contractor";
                                        printLabel(labelFullName,labelCompany,labelCitizen,labelType);
                                    }else if($("input[name='usCitizen']:checked").val()==0){
                                        var labelFullName = $('#fName').val()+" "+$('#lName').val();
                                        var labelCompany = $('#companyName').val();
                                        var labelCitizen = "false";
                                        var labelType = "contractor";
                                        printLabel(labelFullName,labelCompany,labelCitizen,labelType);
                                    }
                                } // valid person
                            } //end of success function
                        }) //end of ajax function
                    }else {
                        return false; // form invalid
                    }
                }

Open in new window

0
 
PhillipsPlasticsAuthor Commented:
No such luck for that one, if I add in a "return false" on line 28 before the valid function else then it works for the verification, however it stops the form from submitting when the data is valid as well.
0
 
leakim971PluritechnicianCommented:
Try this one :

function showRequest() {
    if ($("#visitorSignin").valid()) {
        var data = $.ajax({ async: false, url: 'checkDeniedPersonsList.php', data: 'lName=' + $('#lName').val() }).responseText;
                if (data == 'true') {
                    showWarningFlash("You are on the denied persons list please present your crendentials to the receptionist");
                    return false; //a denied person
                } else if (data == 'false') {
                    if ($("input[name='usCitizen']:checked").val() == 1) {
                        var labelFullName = $('#fName').val() + " " + $('#lName').val();
                        var labelCompany = $('#companyName').val();
                        var labelCitizen = "true";
                        var labelType = "contractor";
                        printLabel(labelFullName, labelCompany, labelCitizen, labelType);
                    } else if ($("input[name='usCitizen']:checked").val() == 0) {
                        var labelFullName = $('#fName').val() + " " + $('#lName').val();
                        var labelCompany = $('#companyName').val();
                        var labelCitizen = "false";
                        var labelType = "contractor";
                        printLabel(labelFullName, labelCompany, labelCitizen, labelType);
                    }
                } // valid person

    } else {
        return false; // form invalid
    }
}

Open in new window

0
 
PhillipsPlasticsAuthor Commented:
Awesome! It appears to be working, can you explain it for me and why mine didn't work and yours does at all?  I would like to try to understand these items better.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now