-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalidation.coffee
127 lines (110 loc) · 3.3 KB
/
validation.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
$(document).ready ->
###
RegEx
###
emailRegEx = new RegExp(/^((?!\.)[a-z0-9._%+-]+(?!\.)\w)@[a-z0-9-\.]+\.[a-z.]{2,5}(?!\.)\w$/i)
emptyRegEx = new RegExp(/[-_.a-zA-Z0-9]{3,}/)
numberRegEx = new RegExp(/^[0-9]{3,}$/)
postalCodeRegEx = new RegExp(/^[A-Z]{1}[0-9]{1}[A-Z]{1} [0-9]{1}[A-Z]{1}[0-9]{1}/)
###
Arrays of inputs, by types
###
inputs = []
emails = []
codes = []
selects = []
choices = [$("#premier-choix"), $("#deuxieme-choix"), $("#troisieme-choix"), $("#quatrieme-choix")]
numbers = []
###
Fetching and sorting all form inputs
###
allinputs = $(".validate").filter(":input")
for input in allinputs
if $(input).hasClass("text")
inputs.push($(input))
if $(input).hasClass("email")
emails.push($(input))
if $(input).hasClass("code")
codes.push($(input))
if $(input).hasClass("select")
selects.push($(input))
if $(input).hasClass("number")
numbers.push($(input))
###
Inputs onblur validation
###
for input in inputs
input.blur () ->
validateInputs($(this), emptyRegEx)
###
Email onblur validation
###
for email in emails
email.blur () ->
validateInputs($(this), emailRegEx)
###
Postal Code onblur validation
###
for code in codes
code.blur () ->
validateInputs($(this), postalCodeRegEx)
###
Selects onchange validation
###
for select in selects
select.change () ->
validateSelect($(this))
###
Numbers onblur validation
###
for number in numbers
number.blur () ->
validateInputs($(this), numberRegEx)
validateForm = () ->
$.extend(badFields = [], validateInputs(inputs, emptyRegEx), validateInputs(emails, emailRegEx), validateInputs(codes, postalCodeRegEx), validateSelect(selects), validateInputs(numbers, numberRegEx), validateChoiceSelect(choices))
if badFields.length is 0
valid = true
else
valid = false
return valid
validateInputs = (inputs, regex) ->
error = []
for input in inputs
if regex.test($(input).val())
removeErrorStyle(input)
else
error.push($(input).attr("id"))
addErrorStyle(input)
return error
validateSelect = (selects) ->
error = []
for select in selects
if $(select).val() isnt "0"
removeErrorStyle(select)
else
error.push($(select).attr("id"))
addErrorStyle(select)
return error
validateChoiceSelect = (choices) ->
error = []
for choice in choices
current = choice
for verif in choices
if($(current).attr("id") is $(verif).attr("id") or $(current).val() isnt $(verif).val())
else
error.push($(current).attr("id"))
$("#error-choice").html(errorMessages['choices'])
if error.length is 0
$("#error-choice").html("")
return error
###
Error Styling, I changed the border of the input and put an error message within a span in the label of the same input, it's opt to you.
###
addErrorStyle = (element) ->
$(element).addClass('form-error')
$(element).prev('label').find('.error-message').html(errorMessages[$(element).attr("id")])
removeErrorStyle = (element) ->
$(element).removeClass('form-error')
$(element).prev('label').find('.error-message').html("")
$('.validate-form').submit ->
return validateForm()