无法使用jquery验证验证特定帐号

时间:2016-09-13 17:29:11

标签: jquery jquery-validate

我正在尝试验证帐号是否为00000000然后抛出验证错误,我需要一些帮助或指示如何对特定数值进行此验证或限制用户输入00000000?

/news[/{params:.*}]

2 个答案:

答案 0 :(得分:0)

您需要value != params[0](不等于)才有效。

请参阅下面的工作示例: -

jQuery.validator.addMethod("myRule", function(value, element, params) {
  return this.optional(element) || value != params[0];
}, "Account number 00000000 is invalid");

$('#myform').validate({ // initialize the plugin
  submitHandler: function(form) {
    console.log("Submitted!");
  },
  rules: {
    accountNumber: {
      required: true,
      minlength: 8,
      maxlength: 8,

      /*MyRule solution found online but not working as expected any other option I could try within JQUERY Validator????*/

      myRule: ["00000000"],
      digits: true
    },
    companyNumber: {
      required: true,
      digits: true

    }
  },

  messages: {
    accountNumber: {
      digits: 'Account Number must be an number',
      minlength: 'Account number cannot be less than 8 digits',
      maxlength: 'Account number cannot be more than 8 digits',
      required: 'Account number is required'
    },
    companyNumber: {
      digits: 'Company number must be a number',
      required: 'Company number is required'

    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.js"></script>

<form id="myform">
  <input type="text" name="accountNumber" />
  <input type="text" name="companyNumber" />
  <input type="submit" value="submit"/>
  </form>

答案 1 :(得分:-1)

您可以使用名为$.validator.addMethod('accountNumber', function (value) { return !(/^0{8}$/.test(value)); }, 'Please enter a valid account.'); 的验证功能。

fputs(tolower(clef[c-idx]), fp);

这是一个带有示例

fiddle