Skip to content

Commit

Permalink
Add scripts, build js.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 14, 2017
1 parent 883fd0d commit 75d5ad9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
32 changes: 31 additions & 1 deletion dist/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@
/**
* @descrition 匹配 URL
*/
url: /[a-zA-z]+:\/\/[^\s]/
url: /[a-zA-z]+:\/\/[^\s]/,
money: /^(0|[1-9]\d*)(\.\d+)?$/,
english: /^[A-Za-z]+$/,
chinese: /^[\u0391-\uFFE5]+$/,
percent: /^(?:[1-9][0-9]?|100)(?:\.[0-9]{1,2})?$/
};
var _testHook = {
// 验证合法邮箱
Expand All @@ -96,6 +100,18 @@
is_url: function(field) {
return regexs.url.test(backVal(field));
},
is_money: function(field) {
return regexs.money.test(backVal(field));
},
is_english: function(field) {
return regexs.english.test(backVal(field));
},
is_chinese: function(field) {
return regexs.chinese.test(backVal(field));
},
is_percent: function(field) {
return regexs.percent.test(backVal(field));
},
// 是否为必填
required: function(field) {
var value = backVal(field);
Expand All @@ -119,6 +135,20 @@
var value1 = backVal(field);
var value2 = backVal(this.fields[newField].element);
return value1 == value2;
},
// 拒绝与某个字段相等,比如登录密码与交易密码情况
different: function(field, newField) {
return !this.same(field, newField);
},
// 直接判断字符串是否相等
contains: function(field, value) {
var value1 = backVal(field);
return value1 == value;
},
// 用于服务条款,是否同意时相当有用,不限制checkbox与radio,有可能submit button直接附带value情况
accepted: function(field) {
var value = backVal(field);
return "YES" == value.toUpperCase() || "ON" == value.toUpperCase() || 1 == value || false == value ? true : false;
}
};
var Validator = function(formelm, fields, callback) {
Expand Down
2 changes: 1 addition & 1 deletion dist/validator.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"main": "dist/validator.js",
"scripts": {
"test": "karma start --single-run",
"test:watch": "karma start",
"clean": "rm -rf dist/validator*.js",
"build:min": "umd Validator src/validator.js | uglifyjs -mc | bannerjs -o > dist/validator.min.js",
"build:dist": "umd Validator src/validator.js | bannerjs -m | uglifyjs -b beautify=true --comments 'all' > dist/validator.js ",
Expand Down Expand Up @@ -45,4 +46,4 @@
"umd": "^3.0.1",
"webpack": "^3.5.6"
}
}
}
40 changes: 37 additions & 3 deletions test/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,47 @@ describe("validators", function () {
expect(v.isTel('13012341233')).to.be.false;
});

// 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)
it("isMoney() 座机号码验证", function () {
// 货币金额
it("isMoney() 货币金额验证", function () {
expect(v.isMoney('086-021-4433432-233')).to.be.false;
expect(v.isMoney('5.00')).to.be.true;
expect(v.isMoney('5')).to.be.true;
expect(v.isMoney('05.00')).to.be.false;
expect(v.isMoney('05.002')).to.be.false;
expect(v.isMoney('5')).to.be.true;
expect(v.isMoney('12,000,000,000')).to.be.false;
});

// 是否为26个字母
it("isEnglish() 是否为26个字母", function () {
expect(v.isEnglish('086-021-4433432-233')).to.be.false;
expect(v.isEnglish('dsf123')).to.be.false;
expect(v.isEnglish('Hello Wold')).to.be.false;
expect(v.isEnglish('Hello')).to.be.true;
});

// 判断是否为中文
it("isChinese() 判断是否为中文", function () {
expect(v.isChinese('086-021-4433432-233')).to.be.false;
expect(v.isChinese('dsf123')).to.be.false;
expect(v.isChinese('Hello Wold')).to.be.false;
expect(v.isChinese('中文')).to.be.true;
expect(v.isChinese('中文!')).to.be.true;
expect(v.isChinese('中文!“我家”')).to.be.true;
expect(v.isChinese('中文!“我家”!,。、()【】‘’《》;')).to.be.true;
});

// 判断百分值
it("isPercent() 判断是否为中文", function () {
expect(v.isPercent('100')).to.be.true;
expect(v.isPercent('100.23')).to.be.true;
expect(v.isPercent('100.12')).to.be.true;
expect(v.isPercent('23.12')).to.be.true;
expect(v.isPercent('1.99')).to.be.true;
expect(v.isPercent('1.2')).to.be.true;
expect(v.isPercent('0.2')).to.be.false;
expect(v.isPercent('101')).to.be.false;
expect(v.isPercent('01')).to.be.false;
expect(v.isPercent(-101)).to.be.false;
});

});

0 comments on commit 75d5ad9

Please sign in to comment.