Tara
Tara

Reputation: 610

Parsley JS force Field to fail validate status

I'm validating a form using parsley JS. When user register on the system, System checks whether his police_id is exists in the system or not. Here that AJAX Jquery code.

//police_id
$('#add_user  input[name=police_id]').keyup(function (e) {
    $('.exists_police_id').hide();
    $('.ok_police_id').hide();
    var site_path = $('#sitePath').val();
    var value =  $(this).val();
    var field = 'policeId';
    var table_name = 'tbl_users';
    if(value.length>9 ){
        $('.loading_pic_police_id').show();
        $.ajax({
            url:site_path +'/users_manage_cont/check_unique_fields',
            method:'POST',
            data:{table_name:table_name,field:field,value:value},

            success:function(data)
            {
                $('.loading_pic_police_id').hide();

                if(data ==''){
                    $('.ok_police_id').show();
                }else{ 
                    $('input[name=police_id]').parsley().addError('forcederror', {message: ' This Police ID already Exists in the System.', updateClass: true});
                    return false; //this validation fails
                }
            }

        });
    }
});

If police_id not exists response data is null. Then allow user to submit the form. But the problem is when police_id exists in the table,The form should be validate=false. Here look my else{} part

 $('input[name=police_id]').parsley().addError('forcederror', {message: ' This Police ID already Exists in the System.', updateClass: true});
                return false; //this validation fails

This set Error message and field color red. But when click submit button the form can be submitted. How to make parsley validate status false when there is existing police_id found in the table?

Upvotes: 1

Views: 525

Answers (1)

Dieter Schmitt
Dieter Schmitt

Reputation: 471

You are only showing an error message, but you are not actually making the validation fail.

Here's my suggestion:

  1. Write the return value from AJAX to a hidden input field
  2. Test you police_id input field with parsley to be not equal to the value of that hidden field.

I made a jsfiddle. If you enter "12345" (your value of course should come from AJAX) it will give you an error message and not validate. If you enter something else it will validate. It's not perfect, but it should give you an impression what i mean.

jsfiddle.net/68y7nc0t/

Regards

Upvotes: 1

Related Questions