-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
dist: trusty | ||
sudo: true | ||
|
||
language: node_js | ||
node_js: | ||
- "7" | ||
- "8" | ||
- "9" | ||
|
||
script: | ||
- npm test | ||
|
||
notifications: | ||
email: | ||
on_success: never | ||
on_failure: never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict'; | ||
|
||
module.exports = class formValidation { | ||
|
||
constructor() { | ||
this.response = { | ||
message: "", | ||
status: 200 | ||
}; | ||
|
||
this.messageContext = { | ||
isString: "請輸入字串", | ||
isEmpty: "請輸入值再進行驗證" | ||
} | ||
} | ||
|
||
empty(input) { | ||
if (typeof input === "undefined" || input === "") { | ||
this.response.message = this.messageContext.isEmpty | ||
this.response.status = 511 | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
userName(userName) { | ||
if (this.empty(userName)) { | ||
return this.response | ||
} | ||
|
||
if (typeof userName !== 'string') { | ||
// throw new TypeError('Expected a string'); | ||
this.response.message = this.messageContext.isString | ||
this.response.status = 510 | ||
return this.response | ||
} | ||
|
||
const rule = new RegExp("^[\u4e00-\u9fa5_a-zA-Z]+$"); | ||
if (!rule.test(userName)) { | ||
this.response.status = 512 | ||
this.response.message = '您的姓名不該包含數字。' | ||
return this.response | ||
} | ||
return this.response | ||
} | ||
|
||
mobile(mobile) { | ||
if (this.empty(mobile)) { | ||
return this.response | ||
} | ||
|
||
const rule = new RegExp("^[09]{2}[0-9]{8}$"); | ||
if (!rule.test(mobile)) { | ||
this.response.status = 512 | ||
this.response.message = '您輸入的手機格式不正確。' | ||
return this.response | ||
} | ||
return this.response | ||
} | ||
|
||
email(email) { | ||
if (this.empty(email)) { | ||
return this.response | ||
} | ||
|
||
const rule = new RegExp("^[^\s]+@[^\s]+\.[^\s]+$"); | ||
if (!rule.test(email)) { | ||
this.response.status = 512 | ||
this.response.message = '您輸入Email格式不正確。' | ||
return this.response | ||
} | ||
return this.response | ||
} | ||
|
||
idcard(idcard) { | ||
if(this.empty(idcard)) { | ||
return this.response | ||
} | ||
|
||
const rule = new RegExp("^[A-Z]{1}[0-9]{9}$"); | ||
if (!rule.test(email)) { | ||
this.response.status = 512 | ||
this.response.message = '您輸入身分證字號格式不正確。' | ||
return this.response | ||
} | ||
return this.response | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "form-validation", | ||
"version": "1.0.1", | ||
"description": "Validate each field data format in JavaScript ", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node ./test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/YutangShi/form-validation.git" | ||
}, | ||
"keywords": [ | ||
"form", | ||
"validation" | ||
], | ||
"author": "AllenShi", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/YutangShi/form-validation/issues" | ||
}, | ||
"homepage": "https://github.com/YutangShi/form-validation", | ||
"devDependencies": { | ||
"assert": "^1.4.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const formValidation = require('./index'); | ||
const assert = require('assert'); | ||
const form = new formValidation(); | ||
|
||
(function testUserName(data1, data2) { | ||
// assert.equal(data1, data2, `not good`); | ||
const res = form.userName("Allen"); | ||
console.log('姓名驗證:' + JSON.stringify(res)) | ||
assert.deepEqual( | ||
res, { status: 200, message: "" } | ||
) | ||
})() | ||
|
||
|
||
// var res = form.userName('1103F'); | ||
// var res = form.email('[email protected]'); | ||
// console.log('res:' + JSON.stringify(res)) |