chan
chan

Reputation: 274

Jquery validate regex not working

I want to check whether an input box has only numbers or not. I tried using regex as well as pattern. It doesn't give me any error messages. Can someone help me out? Thanks.

Here's my code

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript"
        src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js">
</script>

<form id="signupForm" class="col s6 m6 offset-m3 offset-s3" th:action="@{/signup}" method="post">
  <div class="col s12">
    <div class="row">
      <div class="input-field col s12 m10 offset-m1 offset-s2">
        <input id="universityid" name="universityId" th:value="${user.universityId}"
               minlength="6" maxlength="6" required="required" type="text" class="validate"/>
        <label for="universityid">University Id(Numbers Only)</label>
      </div>
    </div>
  </div>
</form>

Here's my jquery code

$('#signupForm').validate({
    rules: {
        universityId: {
            pattern: /^[0-9]+$/
        }
    },
    messages: {
        universityId: {
            pattern: "Only numbers are allowed"
        }
    }
});

Upvotes: 2

Views: 3563

Answers (1)

ndoes
ndoes

Reputation: 678

Apparently you need to add the additional-methods js file.

<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js"></script>

Working fiddle.

Upvotes: 2

Related Questions