0

I'm trying to focus a form element on mouseup or click after a form validation function has been called on mousedown. I can't figure out how to focus the invalid form element on one click of the submit button - which validates the form.

"#submit_button mousedown" : function(el, ev) { 
        if(!validateForm()){
            return;
        }
"validateForm": function(){
        if($('#firstName').val().length<1){
            $('#firstName').next('label.error').remove();
            $('#firstName').addClass('text-error');
            $('#firstName').closest('div.control-group').children('label.control-label').addClass('label-error');
            $('#firstName').after('<label class="error" data-i18n="account.field_required"></label>'); 
            $('label.error').i18n();
            return false;
        } else {
            return true;
        }
}

"#myForm input blur": function(el, ev){           
        var currentId = el[0].id;
        var currentValue = el.val();
        $(el).next('label.error').remove();
        if($(el).is('input')){ $(el).removeClass('text-error'); }
        if($(el).is('select')){ $(el).removeClass('select-error'); }
        $(el).closest('div.control-group').children('label.control-label').removeClass('label-error');
        if(currentId=="firstName" || currentId=="lastName"){
            if(currentValue.length<1){
                $('#'+currentId).addClass('text-error');
                $('#'+currentId).closest('div.control-group').children('label.control-label').addClass('label-error');
                $('#'+currentId).after('<label class="error" data-i18n="account.field_required"></label>');
                $('label.error').i18n();
        } else {
            return;
        }
}

"#submit_button mouseup" : function(el, ev) { 
    steal.dev.log('mouseup called');
    if(!validateForm()){
        steal.dev.log('account form invalid');
        var fieldErrors = ($('#myForm input.text-error').length>0) ? $('#myForm input.text-error') : $('#myForm input.select-error');
        $('#'+fieldErrors[0].id).focus();
        return;          
    }
},

I don't know why the invalid element is not focused on mouseup unless clicked twice.

Thanks,

J

neridaj
  • 2,143
  • 9
  • 31
  • 62
  • Can you put all code. Including html. – prakashpoudel May 21 '13 at 18:17
  • Better yet make a http://www.jsfiddle.net example please! – Mark Pieszak - Trilon.io May 21 '13 at 18:17
  • It turns out I was better off without jQuery Validator for this. I wound up having one method that adds error classes and returns an error string on mousedown, which, if is not equal to true, will bind mouseup and mouseleave to focus invalid elements and append error labels: http://jsfiddle.net/neridaj/zugpx/16/ – neridaj May 29 '13 at 18:26

0 Answers0