1

How can i perform this jquery validation with input type button ? when i switch to input type submit it's working, but i don wan input type submit because submit button will always reload my screen with modernizr.custom.js UI. Please help, thanks.

<script src="js/modernizr.custom.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
         $(document).ready(function() {
           $('#submit').click(function (e) {
               var isValid = true;
               $('#VEHNO').each(function () {
                if ($.trim($(this).val()) == '') {
                    isValid = false;
                    $(this).css({
                           "border": "1px solid red",
                           "background": "#FFCECE"
                           });
                  }
                 else {
                     $(this).css({
                             "border": "",
                             "background": ""
                                });
                      }
                 });
                 if (isValid == false)
                      e.preventDefault();
                   });
     });
       </script>

html

<input type="text" class="input-name" id="VEHNO" name="VEHNO" type="text" />
<input type="button" value="Submit" name="submit"/>
1
  • Keep in mind, that a form can be submitted without using the submit button. For example pressing the Enter key. Commented Jul 22, 2015 at 11:16

3 Answers 3

2

You are trying to use jQuery Id selector for a button but you have not assigned an id to your button, after you attach an id, it will work fine.

    $(document).ready(function () {

         $('#someId').click(function () {

             var isValid = true;

             $('#VEHNO').each(function () {
                 if ($.trim($(this).val()) == '') {
                     isValid = false;
                     $(this).css({
                         "border": "1px solid red",
                             "background": "#FFCECE"
                     });
                 } else {
                     $(this).css({
                         "border": "",
                             "background": ""
                     });
                 }
             });

             if (isValid == false)
                          e.preventDefault();

         });


     });

<input type="text" class="input-name" id="VEHNO" name="VEHNO" type="text" />
<input type="button" value="Submit" id="someId" name="submit" />

Working demo is here : http://jsfiddle.net/qrj0qqrj/

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

1 Comment

Thanks you @Raghvendra Kumar
0
#submit

This is an id selector. It selects the element with the id attribute with the value submit.

<input type="button" value="Submit" name="submit"/>

This element doesn't have an ID.

The name isn't being used for anything. Change name to id.

<input type="button" value="Submit" id="submit"/>

Comments

-1
<input type="button" value="Submit" name="submit" id="submit" />

You can also add a return false; to the JS to stop the form submit and control it by JS.

Comments

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.