2

I'm using this jQuery form validator but I cannot understand how to make one simple thing here: if form is not valid disable the submit button, otherwise enable submit button.

A little bit of code:

<script type="text/javascript">
$(document).ready(function() {
    $("#registration_form").validationEngine({
        //
    });
});
</script>

<?php
echo "<form id=\"registration_form\">";

echo "<label for=\"name\">Name</label>";
echo "<input class=\"validate[required,custom[onlyLetter],length[3,100]]\" id=\"name\"><br />";

echo "<button type=\"submit\" id=\"submit_button\">GO</button>";
echo "</form>";
?>

1 Answer 1

3

This is the script

 <script type="text/javascript">
    $(document).ready(function() {
        $("#registration_form").validationEngine(
    {
        inlineValidation: true
        });
    $('.change').change(function() {
        validate();
    });
    validate();
     });
     function validate(){
     var res = $.validationEngine.loadValidation("#name");
     if (!res)
         $("#submit_button").attr('disabled',''); 
     else
         $("#submit_button").attr("disabled","disabled"); 
 }
 </script>

in the body

<input class="validate[required,custom[onlyLetter],length[3,100]] change" 
   id="name" value=""><br />   

try and feedback me

Sign up to request clarification or add additional context in comments.

9 Comments

Hmm, I don't see how this could help me :) I'm trying to enable or disable a button in my form.
This is correct. If you change some value you have to re-validate the form. Please remove -1
Hi and thanks for helping! However, your solution is not working :( I've tried it myself and was expecting to button enables/disables but in this case, button is ALWAYS disabled... Cannot figure out why :( Plus, all errors are showing on page load...
This is how to desire. I try it
Hey Davide, thank you very much for your effort! Now we are near my solution! :) First of all I needed to change change(function() into keyup(function() because I want to enable my button immediately after user starts typing something in input field. I still have 2 'small' problems; first is that I need to validate few input fields in my form, not only the 'name'... Should I create a variable for each of my input fields and then check if all of them are valid or is there a way to check them all at once?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.