forked from pariweshntt/ntt-flutter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.dart
141 lines (132 loc) · 3.73 KB
/
main.dart
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
GlobalKey<FormState> _key = new GlobalKey();
bool _validate = false;
String name, email, mobile;
final logger = Logger(
printer: PrettyPrinter(
methodCount: 0,
errorMethodCount: 5,
lineLength: 50,
colors: true,
printEmojis: true,
printTime: false,
));
@override
Widget build(BuildContext context) {
return MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Form Validation'),
),
body: new SingleChildScrollView(
child: new Container(
margin: new EdgeInsets.all(15.0),
child: new Form(
key: _key,
autovalidate: _validate,
child: FormUI(),
),
),
),
),
);
}
Widget FormUI() {
return Column(
children: <Widget>[
new TextFormField(
decoration: new InputDecoration(hintText: 'Full Name'),
maxLength: 32,
validator: validateName,
onSaved: (String val) {
name = val;
},
),
new TextFormField(
decoration: new InputDecoration(hintText: 'Mobile Number'),
keyboardType: TextInputType.phone,
maxLength: 10,
validator: validateMobile,
onSaved: (String val) {
mobile = val;
}),
new TextFormField(
decoration: new InputDecoration(hintText: 'Email ID'),
keyboardType: TextInputType.emailAddress,
maxLength: 32,
validator: validateEmail,
onSaved: (String val) {
email = val;
}),
new SizedBox(height: 15.0),
new TextFormField(
decoration: new InputDecoration(hintText: 'Email ID'),
keyboardType: TextInputType.emailAddress,
maxLength: 32,
validator: validateEmail,
onSaved: (String val) {
email = val;
}),
RaisedButton(
onPressed: _sendToServer,
child: new Text('Send'),
)
],
);
}
String validateName(String value) {
String patttern = r'(^[a-zA-Z ]*$)';
RegExp regExp = new RegExp(patttern);
if (value.length == 0) {
return "Name is Required";
} else if (!regExp.hasMatch(value)) {
return "Name must be a-z and A-Z";
}
return null;
}
String validateMobile(String value) {
String patttern = r'(^[0-9]*$)';
RegExp regExp = new RegExp(patttern);
if (value.length == 0) {
return "Mobile is Required";
} else if (value.length != 10) {
return "Mobile number must 10 digits";
} else if (!regExp.hasMatch(value)) {
return "Mobile Number must be digits";
}
return null;
}
String validateEmail(String value) {
String 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 regExp = new RegExp(pattern);
if (value.length == 0) {
return "Email is Required";
} else if (!regExp.hasMatch(value)) {
return "Invalid Email";
} else {
return null;
}
}
_sendToServer() {
if (_key.currentState.validate()) {
// No any error in validation
_key.currentState.save();
logger.i("Name $name");
print("Mobile $mobile");
print("Email $email");
} else {
// validation error
setState(() {
_validate = true;
});
}
}
}