自整理幾個jquery.Validate驗證正則:javascript
1. 只能輸入數字和字母 /^[0-9a-zA-Z]*$/gjava
jQuery.validator.addMethod("letters", function (value, element) { return this.optional(element) || /^[0-9a-zA-Z]*$/g.test(value); }, "只能輸入數字和字母!");
2. 數量必須是正整數 /^\+?[1-9]\d*$/jquery
jQuery.validator.addMethod("count", function (value, element) { return this.optional(element) || /^\+?[1-9]\d*$/.test(value); }, "數量必須是正整數!");
3. 只能輸入非負整數 /^([1-9]\d*|[0])$/git
jQuery.validator.addMethod("counter", function (value, element) { return this.optional(element) || /^([1-9]\d*|[0])$/.test(value); }, "只能輸入非負整數!");
4. 日期爲YYYY-MM-DD格式! /^(\d{4})-(\d{2})-(\d{2})$/this
jQuery.validator.addMethod("dateinput", function (value, element) { return this.optional(element) || /^(\d{4})-(\d{2})-(\d{2})$/.test(value); }, "日期爲YYYY-MM-DD格式!");
5. 溫度爲-100.00到100.00 /^-?(\d|[1-9]\d|100)(\.\d{1,2})?$/spa
jQuery.validator.addMethod("heatlevel", function (value, element) { return this.optional(element) || /^-?(\d|[1-9]\d|100)(\.\d{1,2})?$/.test(value); }, "溫度爲-100.00到100.00");
6. 溼度爲0到100.00 /^(\d|[1-9]\d|100)(\.\d{1,2})?$/code
jQuery.validator.addMethod("moisture", function (value, element) { return this.optional(element) || /^(\d|[1-9]\d|100)(\.\d{1,2})?$/.test(value); }, "溼度爲0到100.00");
7. 請輸入大於0的整數或者保留三位小數blog
jQuery.validator.addMethod("digitals", function (value, element) { //請輸入大於0的整數或者保留三位小數 return this.optional(element) || /(^[1-9]{1}[0-9]*$)|(^[0-9]*\.[0-9]{3}$)/.test(value); }, "發票金額不得小於等於0");
8. 發票號包含數字,-,/ 三種字符,不包含空格ip
jQuery.validator.addMethod("invoices", function (value, element) { return this.optional(element) || /^[-/0-9]*$/.test(value); }, "請輸入合法的發票號");