updateFieldStatus()
method to set them as valid fields
const
hasDuplicatedItems
=
function
(
inputArray
)
{
const
obj
=
{
}
;
const
numItems
=
inputArray
.
length
;
const
duplicateRemoved
=
[
]
;
for
(
const
i
in
inputArray
)
{
obj
[
inputArray
[
i
]
]
=
0
;
}
for
(
const
i
in
obj
)
{
duplicateRemoved
.
push
(
obj
[
i
]
)
;
}
return
duplicateRemoved
.
length
===
numItems
;
}
;
// Assume that emailElements is the list of email elements
let
notEmptyCount
=
0
;
const
obj
=
{
}
;
const
duplicateRemoved
=
[
]
;
for
(
const
i
in
emailElements
)
{
const
v
=
emailElements
[
i
]
.
value
;
if
(
v
!==
''
)
{
obj
[
v
]
=
0
;
notEmptyCount
++
;
}
}
for
(
const
i
in
obj
)
{
duplicateRemoved
.
push
(
obj
[
i
]
)
;
}
if
(
duplicateRemoved
.
length
===
0
)
{
// All the items are empty
}
else
if
(
duplicateRemoved
.
length
!==
notEmptyCount
)
{
// The list of emails have duplicated items
}
email[]
, for example.
const
form
=
document
.
getElementById
(
'demoForm'
)
;
const
fv
=
FormValidation
.
formValidation
(
document
.
getElementById
(
'demoForm'
)
,
{
fields
:
{
'email[]'
:
{
validators
:
{
emailAddress
:
{
message
:
'The value is not a valid email address'
}
,
callback
:
{
callback
:
function
(
input
)
{
...
if
(
duplicateRemoved
.
length
===
0
)
{
return
{
valid
:
false
,
message
:
'You must fill at least one email address'
,
}
;
}
else
if
(
duplicateRemoved
.
length
!==
notEmptyCount
)
{
return
{
valid
:
false
,
message
:
'The email address must be unique'
,
}
;
}
// Set all fields as valid
fv
.
updateFieldStatus
(
'email[]'
,
'Valid'
,
'callback'
)
;
return
{
valid
:
true
,
}
;
}
}
,
}
}
}
,
plugins
:
{
...
}
,
}
)
;
<
input
type
=
"
text
"
class
=
"
js-user-email
"
name
=
"
user.email[0]
"
/>
<
input
type
=
"
text
"
class
=
"
js-user-email
"
name
=
"
user.email[1]
"
/>
<
input
type
=
"
text
"
class
=
"
js-user-email
"
name
=
"
user.email[2]
"
/>
<
input
type
=
"
text
"
class
=
"
js-user-email
"
name
=
"
user.email[3]
"
/>
const
form
=
document
.
getElementById
(
'demoForm'
)
;
const
fv
=
FormValidation
.
formValidation
(
document
.
getElementById
(
'demoForm'
)
,
{
fields
:
{
'email[]'
:
{
// All email fields have .js-user-email class
selector
:
'.js-user-email'
,
validators
:
{
...
}
}
}
,
plugins
:
{
...
}
,
}
)
;