Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
marketing
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
marketing-web
marketing
Commits
0819a115
You need to sign in or sign up before continuing.
Commit
0819a115
authored
Sep 18, 2018
by
zhangmeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
1fa3b928
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
0 deletions
+47
-0
validator.js
src/utils/validator.js
+47
-0
No files found.
src/utils/validator.js
0 → 100644
View file @
0819a115
/*策略规则*/
const
strategies
=
{
isNonEmpty
(
value
,
errorMsg
)
{
return
value
===
''
?
errorMsg
:
void
0
},
minLength
(
value
,
length
,
errorMsg
)
{
return
value
.
length
<
length
?
errorMsg
:
void
0
},
isMoblie
(
value
,
errorMsg
)
{
return
!
/^1
(
3|5|7|8|9
)[
0-9
]{9}
$/
.
test
(
value
)
?
errorMsg
:
void
0
},
isEmail
(
value
,
errorMsg
)
{
return
!
/^
\w
+
([
+-.
]\w
+
)
*@
\w
+
([
-.
]\w
+
)
*
\.\w
+
([
-.
]\w
+
)
*$/
.
test
(
value
)
?
errorMsg
:
void
0
}
}
/*Validator类*/
export
default
class
Validator
{
constructor
()
{
this
.
cache
=
[]
//保存校验规则
}
add
(
dom
,
rules
)
{
for
(
let
rule
of
rules
)
{
let
strategyAry
=
rule
.
strategy
.
split
(
':'
)
//例如['minLength',6]
let
errorMsg
=
rule
.
errorMsg
//'用户名不能为空'
this
.
cache
.
push
(()
=>
{
let
strategy
=
strategyAry
.
shift
()
//用户挑选的strategy
strategyAry
.
unshift
(
dom
.
value
)
//把input的value添加进参数列表
strategyAry
.
push
(
errorMsg
)
//把errorMsg添加进参数列表,[dom.value,6,errorMsg]
return
strategies
[
strategy
].
apply
(
dom
,
strategyAry
)
})
}
}
start
()
{
for
(
let
validatorFunc
of
this
.
cache
)
{
let
errorMsg
=
validatorFunc
()
//开始校验,并取得校验后的返回信息
if
(
errorMsg
)
{
//r如果有确切返回值,说明校验没有通过
return
errorMsg
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment