Last active
March 28, 2018 09:42
-
-
Save tfanme/e934c2fb74adc8a46c57f5f7ec728441 to your computer and use it in GitHub Desktop.
常用表单字段类型校验的正则表达式及测试用例
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
import test from 'ava'; | |
import { expect } from 'chai'; | |
import chai from 'chai'; | |
import { forEach } from 'lodash'; | |
test('额度', t => { | |
try { | |
const exp = /^([1-9](\d{0,9})?(\.\d{1,2})?|0(\.\d{1,2}))$/; | |
const goodCases = [ | |
'1', | |
'1.0', | |
'1.1', | |
'1.01', | |
'1.10', | |
'11.10', | |
'9999999999', | |
'999999999', | |
'0.1', | |
'0.10', | |
'0.01' | |
]; | |
const badCases = [ | |
'0', | |
'01', | |
'01.0', | |
'01.01', | |
'01.01', | |
'-1', | |
'-1.0', | |
'-1.10', | |
'-1.01', | |
'99999999999', | |
'abc', | |
'0abc', | |
]; | |
forEach(goodCases, _case => { | |
expect(_case).to.match(exp); | |
}); | |
forEach(badCases, _case => { | |
expect(_case).to.not.match(exp); | |
}); | |
t.pass(); | |
} catch (err) { | |
console.error(err); | |
t.fail(); | |
} | |
}); | |
test('利率', t => { | |
try { | |
const exp = /^([1-9]\d{0,1}(\.\d{1,2})?|0(\.\d{1,2}))$/; | |
const goodCases = [ | |
'1', | |
'1.0', | |
'1.1', | |
'1.01', | |
'1.10', | |
'11.10', | |
'0.1', | |
'0.10', | |
'0.01', | |
'20.12', | |
'30.13', | |
]; | |
const badCases = [ | |
'0', | |
'01', | |
'01.0', | |
'01.01', | |
'01.011', | |
'01.01', | |
'-1', | |
'-1.0', | |
'-1.10', | |
'-1.01', | |
'111', | |
'abc', | |
'0abc', | |
]; | |
forEach(goodCases, _case => { | |
expect(_case).to.match(exp); | |
}); | |
forEach(badCases, _case => { | |
expect(_case).to.not.match(exp); | |
}); | |
t.pass(); | |
} catch (err) { | |
console.error(err); | |
t.fail(); | |
} | |
}); | |
test('日期(1-10000天)', t => { | |
try { | |
const exp = /^[1-9](\d{0,3})?$/; | |
const goodCases = [ | |
'1', | |
'11', | |
'111', | |
'9999', | |
]; | |
const badCases = [ | |
'0', | |
'01', | |
'01.0', | |
'01.01', | |
'01.01', | |
'-1', | |
'-1.0', | |
'-1.10', | |
'-1.01', | |
'99999', | |
'abc', | |
'0abc', | |
]; | |
forEach(goodCases, _case => { | |
expect(_case).to.match(exp); | |
}); | |
forEach(badCases, _case => { | |
expect(_case).to.not.match(exp); | |
}); | |
t.pass(); | |
} catch (err) { | |
console.error(err); | |
t.fail(); | |
} | |
}); | |
test('电话', t => { | |
try { | |
const exp = /(^1[34578]\d{9}$|^(\d{3,4}|\d{3,4}-)\d{7,8}(-\d{1,4})?$)/; | |
const goodCases = [ | |
'18612464242', | |
'17020301244', | |
'01052729284', | |
'010-52729284', | |
'0531-52729284', | |
'053152729284', | |
'0531-5272928', | |
'05315272928', | |
'010-52729284-1', | |
'0531-52729284-1', | |
'053152729284-1', | |
'0531-5272928-1', | |
'05315272928-8', // -1 | |
'010-52729284-12', | |
'0531-52729284-12', | |
'053152729284-12', | |
'0531-5272928-12', | |
'05315272928-82', // -2 | |
'010-52729284-801', | |
'0531-52729284-801', | |
'053152729284-801', | |
'0531-5272928-801', | |
'05315272928-801', // -3 | |
'010-52729284-8012', | |
'0531-52729284-8012', | |
'053152729284-8012', | |
'0531-5272928-8012', | |
'05315272928-8012', // -4 | |
]; | |
const badCases = [ | |
'0105272928422', // 13 byte | |
'010-527292841', | |
'0531-527292841', | |
'0531527292841', | |
'05311-52729281', | |
'0531152729281', | |
'010-527292841-1', | |
'0531-527292841-1', | |
'05315272d92841-1', | |
'0531-52d729281-1', | |
'0531527229282-8', // -1 | |
'010-527292844-12', | |
'05313-52729284-12', | |
'0531527292844-12', | |
'05341-5272928-12', | |
'0531527292845-82', // -2 | |
'010-524729284-801', | |
'0531-527292484-801', | |
'0053152729284-801', | |
'0531-527239328-801', | |
'0531527293283-801', // -3 | |
'010-527293284-8012', | |
'0531-5272933284-8012', | |
'0531527292384-8012', | |
'0531-527294328-8012', | |
'0531527292338-8012', // -4 | |
'abc', | |
'1abc', | |
'1abc1', | |
'1581108022A', | |
]; | |
forEach(goodCases, _case => { | |
expect(_case).to.match(exp); | |
}); | |
forEach(badCases, _case => { | |
expect(_case).to.not.match(exp); | |
}); | |
t.pass(); | |
} catch (err) { | |
console.error(err); | |
t.fail(); | |
} | |
}); | |
test('Email', t => { | |
try { | |
const exp = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/; | |
const goodCases = [ | |
'abc.li@126.com', | |
'abc_li@126.com', | |
'xiaoli@taiyuan.gov.cn', | |
'chen.li.abc@tsinghua.edu.cn', | |
'ldap_wyopenscm@chinabank.com.cn', | |
'princetoad+moka@gmail.com', | |
'91k69c9n8fbb77t@marketplace.amazon.cn' | |
]; | |
const badCases = [ | |
'fsg@ab@com', | |
'fqhu@MAIL.NFSQ.COM.CN', | |
'FQHU@MAIL.NFSQ.COM.CN', | |
]; | |
forEach(goodCases, _case => { | |
expect(_case).to.match(exp); | |
}); | |
forEach(badCases, _case => { | |
expect(_case).to.not.match(exp); | |
}); | |
t.pass(); | |
} catch (err) { | |
console.error(err); | |
t.fail(); | |
} | |
}); | |
test('QQ 号', t => { | |
try { | |
const exp = /^[1-9]\d{4}(\d{1,6})?$/; | |
const goodCases = [ | |
'50506722', | |
'740828276' | |
]; | |
const badCases = [ | |
'abcd', | |
'a12345', | |
'12345a', | |
'123456789012', | |
'1234', | |
]; | |
forEach(goodCases, _case => { | |
expect(_case).to.match(exp); | |
}); | |
forEach(badCases, _case => { | |
expect(_case).to.not.match(exp); | |
}); | |
t.pass(); | |
} catch (err) { | |
console.error(err); | |
t.fail(); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment