2

I am using https://jqueryvalidation.org/ to validate my form on the frontend. The basic "if field is empty - validate" works OK.

But I'd like to the submit button to be initially disabled until a valid email address has been entered. I'd like the button to become enabled as soon as the field becomes valid (on keypress).

So basically I just need to remove the 'btn-disabled' class once its valid.

I'm struggling with the jQuery to add this function/method. Would be grateful if someone can help out.

Heres a slightly simplifed version: http://codepen.io/dagford/pen/kXJpEZ

$(document).ready(function(){

    $("#reset-form").validate({
        rules: {
            emailaddress: {
                required: true,
                email: true
            }

        },
        messages: {
            email: "Please enter a valid email address"
        }

    }); 

});
swisstony
  • 1,667
  • 3
  • 18
  • 27

3 Answers3

2

you can check if the form is valid after entering the email address. Keep you button disabled by default and remove the disabled attribute once you validate the form.

 $("#emailaddress").on("blur", function(){
    if($("#reset-form").valid())
   {
       $("#btn-reset").removeAttr("disabled");
   }
 }); 

Code Pen : http://codepen.io/anon/pen/YWLjKX?editors=1010

Deep
  • 9,594
  • 2
  • 21
  • 33
0

You have attr for this

$('#button1, #button2').attr("disabled", true);
CMartins
  • 3,247
  • 5
  • 38
  • 53
  • Hi Carlos I'm not really concerned with the disabled attribute at this point (although i was planning to add it in later). What i want to know is how to 'do something' (i.e add / remove classes etc) USING the linked validate plugin as soon as a valid email is entered. And by 'soon as' - i mean upon some sort of keypress event – swisstony Jul 25 '16 at 13:08
0

Check this :

<script type="text/javascript">
                 (function() {
                        $('form > input').keyup(function() {
                            var empty = false;
                            $('form > input').each(function() {
                                if ($(this).val() == '') {    // write your code for valid email here
                                    empty = true;
                                }
                            });
                            if (empty) {
                                $('#register').attr('disabled', 'disabled');
                            } else {
                                $('#register').removeAttr('disabled');
                            }
                        });
                    })()
                </script>
Roma
  • 272
  • 1
  • 12