jQuery Validation plugin events

Hi experts,

i'm using the jQuery Validation plugin

I have a html called TestPage.html

my code looks like this

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Sample Using Validation and Masked Input Plugin</title>   
    
    <script src="http://code.jquery.com/jquery-1.7.2.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
    <script src="http://cloud.github.com/downloads/digitalBush/jquery.maskedinput/jquery.maskedinput-1.3.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.js"></script>    

    <style type="text/css">
        label {
            display: block;
        }

        .error {
            color: #f00;
        }
    </style>

    <script type="text/javascript">

        $(document).ready(function () {
            //-------------------------

            // ****** On Page Load event ******
            // on page load disable the button
            $("#Button1").attr({ disabled: "true" });
            // ****** On Page Load event ******

            // ****** textbox on keyup event ******
            $("#TextBox1").on("keyup", function () {
                
                // enable button on keyup
                $('#Button1').prop('disabled', false);
            });
            // ****** textbox on keyup event ******
            
            // ****** custom validation method ******
            jQuery.validator.addMethod("SSNFormat", function (value, element) {
                return this.optional(element) || /^\d{3}-\d{2}-\d{4}$/.test(value);
            }, "Please enter a 9-digit Social Security Number.");
            // ****** custom validation method ******

            // ****** jQuery validation function ******
            $.fn.ready(function () {
                //$(".phone" ).mask({ mask: "(999) 999-9999?x999999" });
                $("#Form1").validate({
                    rules: {

                        //Masked Input with Regular Expression and Required Field Validators
                        TextBox1: {
                            // calls the validator method called SSNFormat which tests ssn format using regex
                            SSNFormat: true,
                            // must be positive digits only
                            min: 1,
                            // is required
                            required: true
                        }

                    },
                    submitHandler: function (form) {
                        alert("submitted.");
                    }
                });
                $.mask.definitions['^'] = '[123456789]';
                $(".ssn").mask("^99-99-9999");
            });
            // ****** jQuery validation function ******


            //-------------------------
        }); // end ready
                
    </script>

</head>
<body>
  
<form id="Form1" method="POST">
        <p>
            TextBox Validation Specifications
        </p>          
        <ol>
            <li>Digits only</li>
            <li>9 digits.</li>
            <li>Do not allow all zeros</li>
            <li>Enable button only after valid social is entered</li>
        </ol>              

    <br />
    <fieldset>
        <legend>Masked Input with Regular Expression and Required Field Validators</legend>
        <label for="TextBox1">Enter your Social: </label>
        <input type="text" id="TextBox1" name="TextBox1" class="ssn" />
    </fieldset>
    <br />
    <input type="submit" value="Submit" id="Button1"/>
</form>
    
</body>
</html>

Open in new window


When I run my page i have a textbox like this:
on page run
On page load the submit button is disabled.

Right now in my code above , the button becomes enabled when a user starts typing in the textbox (on the keyup event).

My textbox is social security number format.

social security number format is 9 digits.

So if a user doesn't type all 9 digits in the textbox they see this message:
message if user doesn't type in all 9 digits.

Anyone know a way that I can enable the button, not on the keyup event like i have it now,
but only after a user finishes typing in all 9 digits?

so then, if a user types in 123-45-6789  
at the moment after the ninth digit is typed in then enable the button at that point
LVL 1
maqskywalkerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
>> not on the keyup event like i have it now,
Its is OK to use onkeyup to do your desired filtering.  You just need to extract the current value, and see if it has all the needed digits.

           // ****** textbox on keyup event ******
            $("#TextBox1").on("keyup", function () {
                var val=$(this).val().replace(/\D/g,'');
				
				if( /^\d{9}$/.test(val))
				{
                	// enable button on keyup
                	$('#Button1').prop('disabled', false);
				}
				else
				{
					//in case the user backspaces, you need to disable button again
					$('#Button1').prop('disabled', true);
				}
            });

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.