jQuery Validate error placement and required text placement

Refresh

6 days ago

Views

4 time

0

I've created a form which I'm using jQuery validate for and I needed to move the error messaging for two of the fields so I found an example on here that showed how to do that and it worked great. Except, now, the "required" fields are not all working. Here is my form code:

<form id="myForm"> 
  <div class="form-group">
    <label for="x_po_num">Account Number</label>
    <input type="text" class="form-control" name="x_po_num" id="x_po_num">

  </div>
  <div class="form-group">
      <label for="x_invoice_num">Invoice Number</label>
      <div class="input-group">
        <div class="input-group-addon">9</div> 
        <input class="form-control" name="x_invoice_num" id="x_invoice_num" data-error="#invError">
      </div>

      <div id="invError"></div>
  </div>        
  <div class="form-group">
      <label for="x_reference_3">Reference Number</label>
      <input class="form-control" name="x_reference_3" id="x_reference_3" placeholder="Optional" >

  </div>
  <div class="form-group">
      <label for="x_amount">Amount</label>
      <div class="input-group">
        <div class="input-group-addon">$</div>              
        <input class="form-control" name="x_amount" id="x_amount" data-error="#amtError">
      </div>
      <div id="amtError"></div>
  </div>

  <input class="btn btn-primary" value="Pay With Your Credit Card" type="submit"> 
</form>

And here is my jQuery validate code:

$( "#myForm" ).validate({
    rules: {
      x_po_num: {
        required: true,
        digits: true,
        minlength: 11,
        maxlength: 11
      },
      x_invoice_num: {
        required: true,
        digits: true,            
        maxlength: 9,
        minlength: 7,
        normalizer: function( value ) {
          var inv = value;
          inv = "9" + inv;             
          return inv;

        } 
      },
      x_reference_3: {
        digits: true,
        maxlength: 9,
      },
      x_amount: {
        required: true,
        number: true
      }
    },
    messages: {

      x_po_num: {
        required: "Please enter an account number",
        minlength: "The account number must be 11 digits long",
        maxlength: "The account number must be 11 digits long",
        digits: "Please enter digits only, no spaces"
      },
      x_invoice_num: {
        required: "Please enter an invoice number",         
        maxlength: "The invoice number must be 10 digits or less",
        minlength: "The invoice number must be at least 8 digits long",
        digits: "Please enter digits only, no spaces"
      },
      x_reference_3: {
        digits: "Please enter digits only",
        maxlength: "The reference number must be 9 digits or less"
      },
      x_amount:{
        required: "Please enter an amount"
      }

    },
    errorPlacement: function(error, element) {
      var placement = $(element).data('error');
      if (placement) {
        $(placement).append(error)
      } else {
        error.insertAfter(element);
      }
}


  });


 });

Here is the problem:

The two fields which I needed the error messaging moved (Invoice Number and Amount) are now in the correct spots. However, if I open the form and hit submit without entering any data, I am expecting the Invoice Number to come up with an error saying "Please enter an invoice number" but it does not. It does not display any error at all.

0 answers