
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
forms-validator
Advanced tools
a lightweight form validator that base on validator-core.
npm install --save forms-validator
import FormValidator from 'forms-validator'
const ruleSet = [
{
name: 'ChinaName',
rules: ['required', 'size:2-24', /^[\u4e00-\u9fa5]+$/],
tips: ['不能为空', '长度为2到24', '不是中文']
},
{
name: 'EnglishName',
rules: ['required', 'size:2-24', /^[a-zA-Z\s]+$/],
tips: ['Required...', 'Should have 2-24 letter', 'Not a valid English Name']
}
]
// assume there are 5 fields need to check of the form
const formValidator = new FormValidation(5, ruleSet)
loaded as a standalone script
<script src="forms-validator.min.js"></script>
<script type="text/javascript">
const formValidator = new FormValidation()
</script>
export default ruleSetFactory (formType) {
switch (formType) {
case 'simpleForm':
return simpleForm()
break
default:
return simpleForm()
}
}
function simpleForm () {
return [
{
name: 'ChinaName',
rules: ['required', 'size:2-24', /^[\u4e00-\u9fa5]+$/],
tips: ['不能为空', '长度为2到24', '不是中文']
},
{
name: 'EnglishName',
rules: ['required', 'size:2-24', /^[a-zA-Z\s]+$/],
tips: ['Required...', 'Should have 2-24 letter', 'Not a valid English Name']
},
{
name: 'Age',
optional: true,
rules: ['gt:18'],
tips: ['age should be large then 18']
},
{
name: 'Email',
rules: ['required', 'email', 'size:24'],
tips: ['不能为空', '不是合法的 Email 地址', 'Email 地址过长']
},
{
name: 'StartDate',
rules: ['required', 'after:2017-10-03'],
tips: ['需要', '2017-10-03号之后']
},
{
name: 'Price',
rules: ['required', 'lt:5000'],
tips: ['错啦错啦']
},
{
name: 'Color',
rules: ['required', 'in:blue,red,orange']
}
]
}
import FormValidator from 'forms-validator'
import ruleSetFactory from './rule-set.factory'
const simpleForm = ruleSetFactory('simpleForm')
simpleFormValidator = new FormValidator(3, simpleForm)
// assume there are 3 fields in this form
simpleFormValidator.changeTotalFields(3)
const result = simpleFormValidator.field('name').check('李狗蛋', 'ChinaName')
console.log(result)
// => {isError: false, isPass: true, name: 'ChinaName'}
/* because there are 3 fields of this form
* but only the field 'name' is checked
* so the status of this form is not passed
*/
console.log(simpleFormValidator.isPass)
// => false
const ageResult = simpleFormValidator.field('manAge').check(19, 'Age')
const emailResult = simpleFormValidator.field('iEmail').check('hwenleung@gmail.com', 'Email')
console.log(ageResult.isPass) // => true
console.log(emailResult.isPass) // => true
console.log(simpleFormValidator.isPass) // => true
// age should great then 18 (gt:18)
const ageResult2 = simpleFormValidator.field('manAge').check(17, 'Age')
console.log(ageResult2.isPass) // => false
console.log(simpleFormValidator.isPass) // => false
totalFields
, amount fileds of the form to checkruleSet
, see the validator-core for detailproperties, if all fields of the form are pass, return true.
name
, unique filed name for the formreturn status of all fields
total
, if the total fields of the form dynamic changed, use this method to change the total fields of validatorsee validator-core for detail
see validator-core for detail
see validator-core for detail
see validator-core for detail
Copyright (c) 2017-present, hwen hwenleung@gmail.com
FAQs
a lightweight form validator that base on validator-core.
The npm package forms-validator receives a total of 3 weekly downloads. As such, forms-validator popularity was classified as not popular.
We found that forms-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.