Flutter Validation


return new Column(
children: <Widget>[
new TextFormField(
decoration: const InputDecoration(labelText: 'Name'),
keyboardType: TextInputType.text,
validator: validateName,
onSaved: (String val) {
_name = val;
},
),
new TextFormField(
decoration: const InputDecoration(labelText: 'Phone'),
keyboardType: TextInputType.phone,
validator: validateMobile,
onSaved: (String val) {
_mobile = val;
},
),
new TextFormField(
decoration: const InputDecoration(labelText: 'Email'),
keyboardType: TextInputType.emailAddress,
validator: validateEmail,
onSaved: (String val) {
_email = val;
},
),
new SizedBox(
height: 10.0,
),
new RaisedButton(
onPressed: _validateInputs,
child: new Text('Validate'),
)
],
);



String validateName(String value) {
if (value.length < 3)
return 'Name must be more than 2 charater';
else
return null
;
}

String validateMobile(String value) {
// Indian Mobile number are of 10 digit only
if (value.length != 10)
return 'Mobile Number must be of 10 digit';
else
return null
;
}

String validateEmail(String value) {
Pattern pattern =
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
RegExp regex = new RegExp(pattern);
if (!regex.hasMatch(value))
return 'Enter Valid Email';
else
return null
;
}

Komentar