diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f36eb82 --- /dev/null +++ b/.travis.yml @@ -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 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..f923223 --- /dev/null +++ b/index.js @@ -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 + } + +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..266d83c --- /dev/null +++ b/package-lock.json @@ -0,0 +1,32 @@ +{ + "name": "form-validation", + "version": "1.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "assert": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/assert/-/assert-1.4.1.tgz", + "integrity": "sha1-mZEtWRg2tab1s0XA8H7vwI/GXZE=", + "dev": true, + "requires": { + "util": "0.10.3" + } + }, + "inherits": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz", + "integrity": "sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=", + "dev": true + }, + "util": { + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", + "integrity": "sha1-evsa/lCAUkZInj23/g7TeTNqwPk=", + "dev": true, + "requires": { + "inherits": "2.0.1" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..a76a845 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..f1b351e --- /dev/null +++ b/test.js @@ -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('0913407921@gmail.com'); +// console.log('res:' + JSON.stringify(res)) \ No newline at end of file