Skip to content

Commit 896eed3

Browse files
committed
Controlled Form Validation
1 parent cba5305 commit 896eed3

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

confusion/src/components/ContactComponent.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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';
33
import { Link } from 'react-router-dom';
44

55
class Contact extends Component{
@@ -15,10 +15,18 @@ class Contact extends Component{
1515
agree: false,
1616
contactType: 'Tel.',
1717
message: '',
18+
19+
touched:{
20+
firstname: false,
21+
lastname: false,
22+
telnum: false,
23+
email: false,
24+
},
1825
};
1926

2027
this.handleSubmit = this.handleSubmit.bind(this);
2128
this.handleInputChange = this.handleInputChange.bind(this);
29+
this.handleBlur = this.handleBlur.bind(this);
2230
}
2331

2432
handleInputChange(event){
@@ -39,8 +47,61 @@ class Contact extends Component{
3947

4048
}
4149

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+
42102

43103
render(){
104+
const errors = this.validate(this.state.firstname, this.state.lastname, this.state.telnum, this.state.email);
44105

45106
return (
46107
<div className="container">
@@ -102,8 +163,12 @@ class Contact extends Component{
102163
<Input type="text" id="firstname" name="firstname"
103164
placeholder="First Name"
104165
value={ this.state.firstname }
166+
valid={errors.firstname === ''}
167+
invalid={errors.firstname !== ''}
168+
onBlur={this.handleBlur('firstname')}
105169
onChange={this.handleInputChange}
106170
/>
171+
<FormFeedback> {errors.firstname} </FormFeedback>
107172
</Col>
108173
</FormGroup>
109174

@@ -114,9 +179,14 @@ class Contact extends Component{
114179
<Input type="text" id="lastname" name="lastname"
115180
placeholder="Last Name"
116181
value={ this.state.lastname }
182+
valid={errors.lastname === ''}
183+
invalid={errors.lastname !== ''}
184+
onBlur={this.handleBlur('lastname')}
117185
onChange={this.handleInputChange}
118186

119187
/>
188+
<FormFeedback> {errors.lastname} </FormFeedback>
189+
120190
</Col>
121191
</FormGroup>
122192

@@ -127,9 +197,14 @@ class Contact extends Component{
127197
<Input type="tel" id="telnum" name="telnum"
128198
placeholder="Telephone number"
129199
value={ this.state.telnum }
200+
valid={errors.telnum === ''}
201+
invalid={errors.telnum !== ''}
202+
onBlur={this.handleBlur('telnum')}
130203
onChange={this.handleInputChange}
131204

132205
/>
206+
<FormFeedback> {errors.telnum} </FormFeedback>
207+
133208
</Col>
134209
</FormGroup>
135210

@@ -140,9 +215,14 @@ class Contact extends Component{
140215
<Input type="email" id="email" name="email"
141216
placeholder="Email"
142217
value={ this.state.email }
218+
valid={errors.email === ''}
219+
invalid={errors.email !== ''}
220+
onBlur={this.handleBlur('email')}
143221
onChange={this.handleInputChange}
144222

145223
/>
224+
<FormFeedback> {errors.email} </FormFeedback>
225+
146226
</Col>
147227
</FormGroup>
148228

0 commit comments

Comments
 (0)