Sooraj Abbasi Sooraj Abbasi - 1 year ago 105
Javascript Question

How prevent user to enter Zero as first digit in html input box using jquery Validation plug-in,

I am working on validation of HTML form using J query Validate plugin, I am want to restrict things so that the user cannot enter zero as first digit.

For example: I have a text box to enter Amount for any item so user can not enter zero as amount.

sam sam
Answer Source

You can perform a custom validation by adding your own validator using addMethod

As you want to restrict the user from entering 0 as a first digit in the text field, it can be achieved using regular expression ^[^0]\d*.

Here is a snippet.

$.validator.addMethod("pattern", function(value, element, regexpr) {
  return regexpr.test(value);
}, "Please enter a valid value.");

  rules: {
    cost: {
      required: true,
      //change regexp to suit your needs
      pattern: /^[^0]\d*$/g
.form-group label.error {
  color: maroon;
<!DOCTYPE html>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">

  <script src=""></script>
  <script src=""></script>

  <link href="" rel="stylesheet" type="text/css" />
  <script src=""></script>

  <title>JS Bin</title>

  <form class="form-inline">
    <div class="form-group">
      <label for="cost">Cost:</span>
        <input class="form-control" id="cost" type="number" name="cost">
    <button type="submit" class="btn btn-default">Submit</button>


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download