Flutter 如何用颤振检查输入的电话号码是否有效?
我正在检查输入的电话号码是否有效。 意思是,如果我输入了世界上不存在的错误号码,那么它会向我显示一个祝酒词,内容是“请输入一个有效的号码” 请检查这张单据 代码段TextFormField验证程序Flutter 如何用颤振检查输入的电话号码是否有效?,flutter,dart,Flutter,Dart,我正在检查输入的电话号码是否有效。 意思是,如果我输入了世界上不存在的错误号码,那么它会向我显示一个祝酒词,内容是“请输入一个有效的号码” 请检查这张单据 代码段TextFormField验证程序 Widget FormUI() { return new Column( children: <Widget>[ new TextFormField( decoration: const InputDecoration(labelTe
Widget FormUI() {
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: 'Mobile'),
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;
}
FormBuilderTextField(
attribute: "age",
decoration: InputDecoration(labelText: "Age"),
validators: [
FormBuilderValidators.numeric(),
FormBuilderValidators.max(70),
],
),
FormBuilderTextField(
attribute: "over_18",
decoration: InputDecoration(labelText: "Are you over 18?"),
validators: [
FormBuilderValidators.required(),
(val){
if(val.toLowerCase() != "yes")
return "The answer must be Yes";
},
],
),
带包装
支持内置和自定义验证程序
Widget FormUI() {
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: 'Mobile'),
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;
}
FormBuilderTextField(
attribute: "age",
decoration: InputDecoration(labelText: "Age"),
validators: [
FormBuilderValidators.numeric(),
FormBuilderValidators.max(70),
],
),
FormBuilderTextField(
attribute: "over_18",
decoration: InputDecoration(labelText: "Are you over 18?"),
validators: [
FormBuilderValidators.required(),
(val){
if(val.toLowerCase() != "yes")
return "The answer must be Yes";
},
],
),
您可以将自己的电话号码验证逻辑放入验证器中,用于基于国家/地区的电话号码格式检查。我认为,
解决方案在我的代码和
我太笨了,都认不出来了
请看代码中的注释
final PhoneCodeAutoRetrievalTimeout autoRetrieval=(String verId){
this.verificationId=verId;
};
final PhoneCodeSent smsCodeSent=(String verId, [int forceCodeResend]){
this.verificationId=verId;
smsCodeDialog(context).then((value){
}).catchError((onError){
print(onError);
});
};
//This will verified you number
final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) {
print("verified");
};
// and if your number doesn't exist or doesn't match with your country code,Then this will show you an error message
final PhoneVerificationFailed verfifailed=(AuthException exception){
print("${exception.message}");
Fluttertoast.showToast(msg: "The number is invalid", backgroundColor: Colors.red);
//Fluttertoast.showToast(msg: "${exception.message}"); //
};
if(phoneNo !=null){
await firebaseAuth.verifyPhoneNumber(
phoneNumber: this.phoneNo,
codeAutoRetrievalTimeout: autoRetrieval,
codeSent: smsCodeSent,
timeout: const Duration(seconds: 10),
verificationCompleted: verificationCompleted,
verificationFailed: verfifailed
);
}
}
你们试过了吗?是的,但我不需要验证器,因为我已经验证过了。只想检查输入的电话号码是否存在?@ShrutiRamnandanSharma您需要拨打该号码我想您应该解释一下什么是“有效号码”?在前端,我们使用正则表达式作为Samcom的答案。在后端,我们不知道存在或不存在哪个号码,但请调用api,或者如果您是ISP,您可以知道世界上是否存在该号码。@ShrutiRamnandanSharma找到任何解决方案了吗?谢谢,但此解决方案无法解决我的问题。请你重新阅读我的帖子好吗?在写作时不支持Dart
final PhoneCodeAutoRetrievalTimeout autoRetrieval=(String verId){
this.verificationId=verId;
};
final PhoneCodeSent smsCodeSent=(String verId, [int forceCodeResend]){
this.verificationId=verId;
smsCodeDialog(context).then((value){
}).catchError((onError){
print(onError);
});
};
//This will verified you number
final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) {
print("verified");
};
// and if your number doesn't exist or doesn't match with your country code,Then this will show you an error message
final PhoneVerificationFailed verfifailed=(AuthException exception){
print("${exception.message}");
Fluttertoast.showToast(msg: "The number is invalid", backgroundColor: Colors.red);
//Fluttertoast.showToast(msg: "${exception.message}"); //
};
if(phoneNo !=null){
await firebaseAuth.verifyPhoneNumber(
phoneNumber: this.phoneNo,
codeAutoRetrievalTimeout: autoRetrieval,
codeSent: smsCodeSent,
timeout: const Duration(seconds: 10),
verificationCompleted: verificationCompleted,
verificationFailed: verfifailed
);
}
}