如何验证电子邮件或电话的相同字段或不为空?
TextFormField(
// validator: ???,
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(16.0),
hintText: "hint_phone_no_email_address",
filled: true,
fillColor: Colors.grey.withOpacity(0.1),
),
),
Run Code Online (Sandbox Code Playgroud)
我想在按下按钮时进行验证
RaisedButton(
onPressed: () {
// call validate function from here.....
},
textColor: Colors.white,
padding: const EdgeInsets.all(0.0),
child: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: <Color>[
Color(0xFF0D47A1),
Color(0xFF1976D2),
Color(0xFF42A5F5),
],
),
),
child:
Text('Next', style: TextStyle(fontSize: 20)),
),
),
Run Code Online (Sandbox Code Playgroud)
请告诉我...
在这里,我使用email_validator来验证电子邮件,并使用正则表达式来验证电话号码。您还可以检查intl_phone_field的电话号码,或libphonenumber(不过,尚不支持 Web 或桌面):
bool isEmail(String input) => EmailValidator.validate(input);
bool isPhone(String input) => RegExp(
r'^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$'
).hasMatch(input);
Run Code Online (Sandbox Code Playgroud)
TextFormField然后,在您的 中TextFormField,定义您测试电子邮件和电话号码的aGlobalKey<FormFieldState>和 a :validator
TextFormField(
key: _key.value,
validator: (value) {
if (!isEmail(value) && !isPhone(value)) {
return 'Please enter a valid email or phone number.';
}
return null;
},
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(16.0),
hintText: "Enter your phone number or email",
filled: true,
fillColor: Colors.grey.withOpacity(0.1),
),
),
Run Code Online (Sandbox Code Playgroud)
TextFormField按下按钮当用户按下按钮时,验证TextFormField并导航(如果有效)。
ElevatedButton(
onPressed: () {
if (_key.value.currentState.validate()) {
// Navigate to next page
}
},
style: ButtonStyle(
padding: MaterialStateProperty.all(const EdgeInsets.all(0.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
backgroundColor: MaterialStateProperty.all(Color(0xFF0D47A1))
),
child: Text('Next', style: TextStyle(fontSize: 20)),
),
Run Code Online (Sandbox Code Playgroud)
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:email_validator/email_validator.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: HomePage(),
),
);
}
bool isEmail(String input) => EmailValidator.validate(input);
bool isPhone(String input) =>
RegExp(r'^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$')
.hasMatch(input);
class HomePage extends HookWidget {
@override
Widget build(BuildContext context) {
final _key = useState(GlobalKey<FormFieldState>());
return Scaffold(
body: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(16.0),
child: Column(
children: [
TextFormField(
key: _key.value,
validator: (value) {
if (!isEmail(value) && !isPhone(value)) {
return 'Please enter a valid email or phone number.';
}
return null;
},
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(16.0),
hintText: "Enter your phone number or email",
filled: true,
fillColor: Colors.grey.withOpacity(0.1),
),
),
const SizedBox(height: 16.0),
ElevatedButton(
onPressed: () {
if (_key.value.currentState.validate()) {
// Navigate to next page
}
},
style: ButtonStyle(
padding: MaterialStateProperty.all(const EdgeInsets.all(0.0)),
foregroundColor: MaterialStateProperty.all(Colors.white),
backgroundColor:
MaterialStateProperty.all(Color(0xFF0D47A1))),
child: Text('Next', style: TextStyle(fontSize: 20)),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16047 次 |
| 最近记录: |