1

I am using Codeigniter and i am creating a login registration form which check the email of user that it available or not. User can login with that id. So I am Trying to use Ajax for it.

So I have Put this Ajax in my View.

            $(document).ready(function() {
            $("#email").keyup(function() {
                var email = $(this).val();

                if (email.length > 3) {
                    $("#result").html('checking...');

                    $.ajax({
                        type: 'POST',
                        url: emailAvailability,
                        data: { email: email },
                        success: function(data) {
                            console.log(data);
                            $("#result").html(data);

                            if (data.indexOf('Available')) {
                                emailAvaliable = 1;
                                //$('#candidateLogin').removeClass('disabled');
                            } else {
                                emailAvaliable = 0;
                                //$('#candidateLogin').addClass('disabled');
                            }
                        }
                    });
                    return false;
                } else {
                    $("#result").html('');
                }
            });
            });

I am Using parsley plugin for validation.

 $(".formValidationClass").on('click', function (e) {
     e.preventDefault;
     $(this).parsley();
 });

Now the Controller Code.

public function check_email_availability(){

    $data = $this->input->post();
    // Now I want to check email is unique or not without going to database.
}

The Second Problem is i want to disable the form till email is available & valid.

I have tried this script to disable the form to submit but it's not working and form get submitted. I have done the server side validation to not submit but still i want to prevent it form the client side. this is the script.

     $(".formValidationClass").on('click', function(e) {
     e.preventDefault;

     $(this).parsley().validate();
     console.log('on form click, After validation');
     //  return false;


 });

 var emailAvaliable = 0;

 $(".formValidationClass").submit(function(event) {
     // Validate form fields

     if (emailAvaliable == 1) {
         console.log('Email Avaliable');
         $(this).parsley();
         return true;
     } else {
         console.log('Email not Avaliable');
         return false;
     }

 });

All the suggestion related improving the code is acceptable. Thanks.

1 Answers1

1

if you want to prevent the form submit event then please use: e.PreventDefault(); Check this Using JQuery - preventing form from submitting for more information. I think this is useful for you.

Matt
  • 5,315
  • 1
  • 30
  • 57
  • Thanks But i have looked that first and tried it also. But i did not get it that how to use it with my parsley validation plugin. –  May 20 '17 at 04:45