1
1
import React , { Component } from 'react' ;
2
- import { Breadcrumb , BreadcrumbItem , Button , Form , FormGroup , Label , Input , Col } from 'reactstrap' ;
2
+ import { Breadcrumb , BreadcrumbItem , Button , Form , FormGroup , Label , Input , Col , FormFeedback } from 'reactstrap' ;
3
3
import { Link } from 'react-router-dom' ;
4
4
5
5
class Contact extends Component {
@@ -15,10 +15,18 @@ class Contact extends Component{
15
15
agree : false ,
16
16
contactType : 'Tel.' ,
17
17
message : '' ,
18
+
19
+ touched :{
20
+ firstname : false ,
21
+ lastname : false ,
22
+ telnum : false ,
23
+ email : false ,
24
+ } ,
18
25
} ;
19
26
20
27
this . handleSubmit = this . handleSubmit . bind ( this ) ;
21
28
this . handleInputChange = this . handleInputChange . bind ( this ) ;
29
+ this . handleBlur = this . handleBlur . bind ( this ) ;
22
30
}
23
31
24
32
handleInputChange ( event ) {
@@ -39,8 +47,61 @@ class Contact extends Component{
39
47
40
48
}
41
49
50
+ handleBlur = ( field ) => ( evt ) => {
51
+ this . setState ( {
52
+ touched : { ...this . state . touched , [ field ] : true }
53
+ } )
54
+ }
55
+
56
+ // checking validation
57
+ validate ( firstname , lastname , telnum , email ) {
58
+ const errors = {
59
+ firstname : '' ,
60
+ lastname : '' ,
61
+ telnum : '' ,
62
+ email : '' ,
63
+ } ;
64
+
65
+ // firstName
66
+ if ( this . state . touched . firstname && firstname . length < 3 )
67
+ {
68
+ errors . firstname = 'First name should be >= 3 chars' ;
69
+ }
70
+ else if ( this . state . touched . firstname && firstname . length >= 10 )
71
+ {
72
+ errors . firstname = 'First name should be <= 10 chars' ;
73
+ }
74
+
75
+ // lastname
76
+ if ( this . state . touched . lastname && lastname . length < 3 )
77
+ {
78
+ errors . lastname = 'Last name should be >= 3 chars' ;
79
+ }
80
+ else if ( this . state . touched . lastname && lastname . length >= 10 )
81
+ {
82
+ errors . lastname = 'Last name should be <= 10 chars' ;
83
+ }
84
+
85
+ // telnum
86
+ const reg = / ^ \d + $ / ; //all the char must be numbers
87
+
88
+ if ( this . state . touched . telnum && ! reg . test ( telnum ) )
89
+ {
90
+ errors . telnum = 'Telephone must be numbers' ;
91
+ }
92
+
93
+ // email
94
+ if ( this . state . touched . email && email . split ( '' ) . filter ( x => x === '@' ) . length !== 1 )
95
+ {
96
+ errors . email = 'Email should contain a @ sign' ;
97
+ }
98
+
99
+ return errors ;
100
+ }
101
+
42
102
43
103
render ( ) {
104
+ const errors = this . validate ( this . state . firstname , this . state . lastname , this . state . telnum , this . state . email ) ;
44
105
45
106
return (
46
107
< div className = "container" >
@@ -102,8 +163,12 @@ class Contact extends Component{
102
163
< Input type = "text" id = "firstname" name = "firstname"
103
164
placeholder = "First Name"
104
165
value = { this . state . firstname }
166
+ valid = { errors . firstname === '' }
167
+ invalid = { errors . firstname !== '' }
168
+ onBlur = { this . handleBlur ( 'firstname' ) }
105
169
onChange = { this . handleInputChange }
106
170
/>
171
+ < FormFeedback > { errors . firstname } </ FormFeedback >
107
172
</ Col >
108
173
</ FormGroup >
109
174
@@ -114,9 +179,14 @@ class Contact extends Component{
114
179
< Input type = "text" id = "lastname" name = "lastname"
115
180
placeholder = "Last Name"
116
181
value = { this . state . lastname }
182
+ valid = { errors . lastname === '' }
183
+ invalid = { errors . lastname !== '' }
184
+ onBlur = { this . handleBlur ( 'lastname' ) }
117
185
onChange = { this . handleInputChange }
118
186
119
187
/>
188
+ < FormFeedback > { errors . lastname } </ FormFeedback >
189
+
120
190
</ Col >
121
191
</ FormGroup >
122
192
@@ -127,9 +197,14 @@ class Contact extends Component{
127
197
< Input type = "tel" id = "telnum" name = "telnum"
128
198
placeholder = "Telephone number"
129
199
value = { this . state . telnum }
200
+ valid = { errors . telnum === '' }
201
+ invalid = { errors . telnum !== '' }
202
+ onBlur = { this . handleBlur ( 'telnum' ) }
130
203
onChange = { this . handleInputChange }
131
204
132
205
/>
206
+ < FormFeedback > { errors . telnum } </ FormFeedback >
207
+
133
208
</ Col >
134
209
</ FormGroup >
135
210
@@ -140,9 +215,14 @@ class Contact extends Component{
140
215
< Input type = "email" id = "email" name = "email"
141
216
placeholder = "Email"
142
217
value = { this . state . email }
218
+ valid = { errors . email === '' }
219
+ invalid = { errors . email !== '' }
220
+ onBlur = { this . handleBlur ( 'email' ) }
143
221
onChange = { this . handleInputChange }
144
222
145
223
/>
224
+ < FormFeedback > { errors . email } </ FormFeedback >
225
+
146
226
</ Col >
147
227
</ FormGroup >
148
228
0 commit comments