Click here to Skip to main content
15,905,071 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am using a jquery to validate the form controlls and to shake them when they are invalid.

When i click submit it shows all the controlls shaking but , it loosing focus from all the controlls. i mean if a input box not filled in it shoud shake and the focus (cursor) should be in that box but now i can manage to do the shake but it is loosing the focus from the input box.

Please can any one help me please.
I am using the following jquery

<script type="text/javascript" language="javascript">

        $().ready(function () {
        
        $('#showLightDiv').hide();
         $('#barcode1Image').hide();
          $('#barcode2Image').hide();
           $('#barcode3Image').hide();
            $('#barcode4Image').hide();
             $('#barcode5Image').hide();
              $('#barcode6Image').hide();
               $('#barcode7Image').hide();
                $('#barcode8Image').hide();
                 $('#barcode9Image').hide();  
                 $('#barcode1ImageWrong').hide(); 
                 $('#barcode2ImageWrong').hide(); 
                 $('#barcode3ImageWrong').hide(); 
                 $('#barcode4ImageWrong').hide(); 
                 $('#barcode5ImageWrong').hide(); 
                 $('#barcode6ImageWrong').hide(); 
                 $('#barcode7ImageWrong').hide(); 
                 $('#barcode8ImageWrong').hide(); 
                 $('#barcode9ImageWrong').hide();  
        
      
            $('#contactForm').validate({ 
                rules: {
                    name: "required",
                    tel: "required",
                    province: "required",
                    catname: "required",
                    email: { required:true , email:true },
                    barcode1: "required",
                    barcode2: "required",
                    barcode3: "required",
                    barcode4: "required",
                    barcode5: "required",
                    barcode6: "required",
                    barcode7: "required",
                    barcode8: "required",
                    barcode9: "required"
                },
                messages: {
                    name: "Please enter your Name",
                    tel: "Please enter your Surname",
                    ddlProvince: "Mobile number is Required",
                    catname: "Cat's name required",
                    email: "Please enter email",
                    barcode1: "barcode required",
                    barcode2: "barcode required",
                    barcode3: "barcode required",
                    barcode4: "barcode required",
                    barcode5: "barcode required",
                    barcode6: "barcode required",
                    barcode7: "barcode required",
                    barcode8: "barcode required",
                    barcode9: "barcode required"
                    
                },
                highlight: function (element, errorClass, validClass) {
                    $(element).stop()
                            .removeClass(validClass)
                            .addClass(errorClass)
                            .removeClass(validClass)
                            .effect('shake', {times: 4, distance: 5}, focus())
                            .css('border-color', 'Red')
                            ;
                           
                },
                unhighlight: function (element, errorClass, validClass) {
                    $(element).stop()
                    .removeClass(errorClass)
                    .addClass(validClass)                                   
                    .css('border-color', '#d2d2d2');
                    

                },
                errorPlacement: function(error,element){
                }

            });
        }); 
    
    </script>
Posted
Updated 15-Mar-11 5:19am
v2
Comments
fjdiewornncalwe 15-Mar-11 13:50pm    
Stop shaking them so hard. They're all probably just passing out. :)
shekhardumala 15-Mar-11 15:09pm    
Hi Thank you for the reply.
But can you explane to me what i have to do.

Thank you

Hi,

Guys i have solved this problem by using the animation effect instead using shake.

Shake can be used to the group of the controlls then that will be good.

i used

highlight: function (element, errorClass, validClass) {
$(element).stop()
.removeClass(validClass)
.addClass(errorClass)
.removeClass(validClass)
.css({position: &#39;relative&#39;})
.animate({ left: -10 },150)
.animate({ left: 0 },150)
.animate({ left: 10 },150)
.animate({ left: 0 },150);
},</pre>

instead of

<pre lang="cs">highlight: function (element, errorClass, validClass) {
$(element).stop()
.removeClass(validClass)
.addClass(errorClass)
.removeClass(validClass)
.effect(&#39;shake&#39;, {times: 4, distance: 5}, focus())
.css(&#39;border-color&#39;, &#39;Red&#39;)
;
},</pre>





and it solved my problem.


Thanks everyone
 
Share this answer
 
I'm not sure what passing in focus() does with your .effect call but I'm willing to bet that is your problem.
Try placing .focus() after your .css call.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900