Skip to content

Commit 6f25ec2

Browse files
committed
React Redux Form Validation
1 parent eb78ce9 commit 6f25ec2

File tree

1 file changed

+65
-6
lines changed

1 file changed

+65
-6
lines changed

confusion/src/components/ContactComponent.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import { Link } from 'react-router-dom';
55

66
import { Control, LocalForm, Errors } from 'react-redux-form';
77

8+
9+
//// validators
10+
const required = (val) => val && val.length; //value > 0
11+
const maxLength = (len) => (val) => !(val) || (val.length <= len);
12+
const minLength = (len) => (val) => (val) &&( val.length >= len);
13+
const isNumber = (val) => !isNaN(Number(val));
14+
const validEmail = (val) => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(val);
15+
16+
17+
818
class Contact extends Component{
919

1020
constructor(props){
@@ -82,8 +92,21 @@ class Contact extends Component{
8292
<Col md={10}>
8393
<Control.text model=".firstname" id="firstname" name="firstname"
8494
placeholder="First Name"
85-
className="form-control"
95+
className="form-control"
96+
validators={{
97+
required, minLength: minLength(3), maxLength: maxLength(15)
98+
}}
8699
/>
100+
<Errors
101+
className="text-danger"
102+
model=".firstname"
103+
show="touched"
104+
messages={{
105+
required: "Required",
106+
minLength: 'Must be greater that 2 characters',
107+
maxLength: 'Must be 15 characters or less',
108+
}}
109+
></Errors>
87110
</Col>
88111
</Row>
89112

@@ -94,8 +117,20 @@ class Contact extends Component{
94117
<Control.text model=".lastname" id="lastname" name="lastname"
95118
placeholder="Last Name"
96119
className="form-control"
120+
validators={{
121+
required, minLength: minLength(3), maxLength: maxLength(15)
122+
}}
123+
/>
124+
<Errors
125+
className="text-danger"
126+
model=".lastname"
127+
show="touched"
128+
messages={{
129+
required: 'Required',
130+
minLength: 'Must be greater than 2 characters',
131+
maxLength: 'Must be 15 characters or less'
132+
}}
97133
/>
98-
99134
</Col>
100135
</Row>
101136

@@ -104,22 +139,46 @@ class Contact extends Component{
104139
<Label htmlFor="telnum" md={2}>Contact Tel.</Label>
105140
<Col md={10}>
106141
<Control.text model=".telnum" id="telnum" name="telnum"
107-
placeholder="Telephone number"
142+
placeholder="Tel. Number"
108143
className="form-control"
144+
validators={{
145+
required, minLength: minLength(3), maxLength: maxLength(15), isNumber
146+
}}
147+
/>
148+
<Errors
149+
className="text-danger"
150+
model=".telnum"
151+
show="touched"
152+
messages={{
153+
required: 'Required',
154+
minLength: 'Must be greater than 2 numbers',
155+
maxLength: 'Must be 15 numbers or less',
156+
isNumber: 'Must be a number'
157+
}}
109158
/>
110-
111159
</Col>
112160
</Row>
113161

114162
{/* email */}
115163
<Row className="form-group">
116164
<Label htmlFor="email" md={2}>Email</Label>
117165
<Col md={10}>
118-
<Control.text model=".email" id="email" name="email"
166+
<Control.text model=".email" id="email" name="email"
119167
placeholder="Email"
120168
className="form-control"
169+
validators={{
170+
required, validEmail
171+
}}
172+
/>
173+
<Errors
174+
className="text-danger"
175+
model=".email"
176+
show="touched"
177+
messages={{
178+
required: 'Required',
179+
validEmail: 'Invalid Email Address'
180+
}}
121181
/>
122-
123182
</Col>
124183
</Row>
125184

0 commit comments

Comments
 (0)