I'm working on this validation script for my latest project, and one of the requirements is that it checks if the value that the user enters is in the database, and if it isn't it returns an error.
function validateSteps(){
var FormErrors = false;
for(var i = 1; i < fieldsetCount; ++i){
var error = validateStep(i);
if(error == -1)
FormErrors = true;
}
$('#formElem').data('errors',FormErrors);
}
function validateStep(step){
if(step == fieldsetCount) return;
var error = 1;
var hasError = false;
$('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
var $this = $(this);
var valueLength = jQuery.trim($this.val()).length;
var inputValue = jQuery.trim($this.val());
var errorID = $this.attr('name') + "_err";
var errorPrepend = "<div class='rf_error' id='" + errorID +"'>";
var errorAppend = "</div>";
var errorMsg = "";
/* =================================
MORE VALIDATION STATEMENTS HERE
============================ */
if($this.is('.rf_GrpCode') && !hasError)
{
$.get("inc/scripts/formHandle.php", { GrpCode: inputValue, type: "groupCode" }, function(data) {
if(!data.GrpCode)
{
hasError = true;
errorMsg = "The code you have entered is invalid.";
}
}, "json");
}
if(hasError)
{
$this.css('background-color','#FFEDEF');
//alert("Has error: " + errorID);
if(errorMsg)
{
if($('#' + errorID).length)
{
$('#' + errorID).html(errorMsg);
}
else
{
$this.after(errorPrepend + errorMsg + errorAppend);
}
}
}
else
{
//alert("Has no error: " + errorID);
$('#' + errorID).remove();
$this.css('background-color','#FFFFFF');
}
});
var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
$link.parent().find('.error,.checked').remove();
var valclass = 'checked';
if(hasError){
error = -1;
valclass = 'error';
}
$('<span class="'+valclass+'"></span>').insertAfter($link);
return error;
}
$('#registerButton').bind('click',function(){
if($('#formElem').data('errors')){
$.confirm({
'title' : 'Ooops!',
'message' : 'It appears some of the information you have entered is invalid. Please go back through the steps a amend the marked fields.',
'buttons' : {
'OK' : {
'class' : 'blue',
'action': function(){}
}
}
});
return false;
}
});
The $.get() request itself works fine and will get through into the if statement if there is no instance of the value in the database. However once it comes to the error handling section it's not picking up the variables that I set within the if statement.
I can understand why it's not working, because those variables are being set out of the scope of the rest of the function. Unfortunately this is as far as my knowledge goes, and am at a loss as to how to get those variables to be recognised by the error handeling section at the end.
Hope that makes sense,
Any help is greatly appreciated, thanks,
Lyndon