开发者

validation to ignore space at beginning of a text field

开发者 https://www.devze.com 2022-12-07 20:53 出处:网络
I try: regno: { required: true, maxlength: 20, noSpace: true } Can somebody assist me to validate text field that the beginning does not allow space?开发者_如何学编程You can use regular expression to

I try:

regno: {
           required: true,
           maxlength: 20,
           noSpace: true
       }

Can somebody assist me to validate text field that the beginning does not allow space?开发者_如何学编程


You can use regular expression to check leading white space with pattern from validation plugin additional method.

Example:

$("#YourForm").validate({
  rules: {
    txtName: {
      pattern: /^(?!\s)/
    },
  },
  messages: {
    txtName: {
      pattern: 'No white space at begining.'
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/additional-methods.min.js"></script>
<form id="YourForm" method="get" action="">
  <label>Textbox</label>
  <input id="txtName" name="txtName" type="text"/>
</form>

Note: Don't forget to add additional-methods.js after validate.js

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号