Link to home
Start Free TrialLog in
Avatar of PhillipsPlastics
PhillipsPlastics

asked on

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

Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Avatar of PhillipsPlastics
PhillipsPlastics

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.