これは、私のウェブサイトにあるフォームを検証する jquery 関数の一部です。
/*
validates errors on all the fieldsets
records if the Form has errors in $('#formElem').data()
*/
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);
}
/*
validates one fieldset
and returns -1 if errors found, or 1 if not
*/
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;
if(valueLength == ''){
hasError = true;
$this.css('background-color','#FFEDEF');
}
else
$this.css('background-color','#A8FC9C'); /* Campo preenchido */
});
var $link = $('#navigation_form 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;
}
/*
if there are errors don't allow the user to submit
*/
$('#enviar_candidatura').bind('click',function(){
var preenchimentoForm=true;
if($('#formElem').data('errors')){
preenchimentoForm=false;
dadosFormularios(preenchimentoForm);
//return false;
}
else{
dadosFormularios(preenchimentoForm);
}
});
});
ただし、クライアント側のフィールドを検証するために LiveValidation (http://livevalidation.com/) も使用しています。明らかに、jquery によって検証された部分が OK であれば、フォームが送信されるため、LiveValidation 部分にエラーがあっても問題ありません。したがって、このjqueryコード内にif句などを追加して、LV_invalid_fieldsがあるかどうかを確認する必要があります。「はい」の場合、jquery検証部分で行われたようにフォームの送信をブロックしますが、その方法もわかりません。上記のコードのどこにこの検証を配置するのが正しいのですか。助けていただければ幸いです。ありがとう!