Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    43
    Thanks
    2
    Thanked 0 Times in 0 Posts

    jQuery: how to validate an input text before submit or change the cursor?

    Hi,

    using jQuery, how to validate a required input text before submitting or placing the cursor in another field?

    I've tried this below but it doesn't work.


    Code:
    $(document).ready(function() {
      $("#form").validate({
        rules: {
          name: "coche4[marca]",
          required: true
          }
        },
        messages: {
          comment: "You must insert something."
        }
      });
    });
    Regards

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,950
    Thanks
    9
    Thanked 724 Times in 718 Posts
    Looks like you've got an extra closing bracket in there:

    Code:
    $(document).ready(function() {
      $("#form").validate({
        rules: {
          name: "coche4[marca]",
          required: true
          }
        },
        messages: {
          comment: "You must insert something."
        }
      });
    });
    ..although, I'm not quite sure your syntax is right anyway. If you have a form like this:

    Code:
    <form id="form" method="post" action="">
    <input name="mynamefield" type="text"/>
    <input name="comment" type="text"/>
    <input type="submit" />
    
    </form>
    then to make both of these elements required and specify a message, you'd need something like this:

    Code:
    $(document).ready(function() {
      $("#form").validate({
        rules: {
          mynamefield:   {
    			  //"coche4[marca]",
    			  required: true
    			  },
    	  comment: "required"	  
        },
        messages: {
          comment: "You must insert something."
        }
      });
    });
    Last edited by SB65; 11-23-2010 at 01:50 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •