javascript - How prevent user to enter Zero as first digit in html input box using jquery Validation plug-in, -
i working on validation of html form using j query validate plugin, want restrict things user cannot enter 0 first digit.
for example: have text box enter amount item user can not enter 0 amount.
you can perform custom validation adding own validator using addmethod
as want restrict user entering 0 first digit in text field, can achieved using regular expression ^[^0]\d*
.
here snippet.
$.validator.addmethod("pattern", function(value, element, regexpr) { return regexpr.test(value); }, "please enter valid value."); $("form").validate({ rules: { cost: { required: true, //change regexp suit needs pattern: /^[^0]\d*$/g } } });
.form-group label.error { color: maroon; }
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script src="https://jqueryvalidation.org/files/dist/jquery.validate.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <title>js bin</title> </head> <body> <form class="form-inline"> <div class="form-group"> <label for="cost">cost:</span> <input class="form-control" id="cost" type="number" name="cost"> </div> <button type="submit" class="btn btn-default">submit</button> </form> </body> </html>
Comments
Post a Comment