Skip to content

Commit

Permalink
upload index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
YutangShi committed Feb 18, 2018
1 parent c8abfa8 commit 423321f
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
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
90 changes: 90 additions & 0 deletions index.js
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
}

}
32 changes: 32 additions & 0 deletions package-lock.json

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

26 changes: 26 additions & 0 deletions package.json
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"
}
}
17 changes: 17 additions & 0 deletions test.js
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))

0 comments on commit 423321f

Please sign in to comment.