user3289108
user3289108

Reputation: 770

Validate a multiple word input separated by space in JQuery validation?

I'm using a jQuery validation-plugin to validate a form's fields. I've got a scenario where we need to validate a field which has a multiple word input name like,

<input type="text" name="total amount">

how should i validate? I tried a normal approach but it doesn't validate. And i can't use class or id as these data's comes from the database.

I can validate it by adding class. But, i have 1000 of entries in database, so it not a good solution.

So, how should i validate a input which has a multiple word name?

Upvotes: 2

Views: 748

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can add the rules by specifying the input name enclosed in '' or ""

jQuery(function($) {
  var validator = $('#myform').validate({
    rules: {
      'total amount': {
        required: true
      }
    },
    messages: {}
  });
});
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/additional-methods.js"></script>


<form id="myform" method="post" action="">

  <input type="text" name="total amount" />

  <input type="submit" value="Save" />
</form>

Upvotes: 2

Related Questions