我创建了一个登录页面,我需要将这些内容添加到我的密码中。如何使用验证警报消息执行此操作?
Vig*_* KM 37
您需要使用正则表达式来验证结构。
bool validateStructure(String value){
String pattern = r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$';
RegExp regExp = new RegExp(pattern);
return regExp.hasMatch(value);
}
output:
Vignesh123! : true
vignesh123 : false
VIGNESH123! : false
vignesh@ : false
12345678? : false
Run Code Online (Sandbox Code Playgroud)
此函数将验证传递的值是否具有结构。
var _usernameController = TextEditingController();
String _usernameError;
...
@override
Widget build(BuildContext context) {
return
...
TextFormField(
controller: _usernameController,
decoration: InputDecoration(
hintText: "Username", errorText: _usernameError),
style: TextStyle(fontSize: 18.0),
),
Container(
width: double.infinity,
height: 50.0,
child: RaisedButton(
onPressed: validate,
child: Text(
"Login",
style: TextStyle(color: Colors.white),
),
color: Theme.of(context).primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0),
),
),
),
...
}
...
validate(){
if(!validateStructure(_usernameController.text)){
setState(() {
_usernameError = emailError;
_passwordError = passwordError;
});
// show dialog/snackbar to get user attention.
return;
}
// Continue
}
Run Code Online (Sandbox Code Playgroud)
Nik*_*ikh 30
您的正则表达式应如下所示:
r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$
Run Code Online (Sandbox Code Playgroud)
这是一个解释:
r'^
(?=.*[A-Z]) // should contain at least one upper case
(?=.*[a-z]) // should contain at least one lower case
(?=.*?[0-9]) // should contain at least one digit
(?=.*?[!@#\$&*~]) // should contain at least one Special character
.{8,} // Must be at least 8 characters in length
$
Run Code Online (Sandbox Code Playgroud)
将上述表达式与您的密码字符串匹配。使用此方法-
String validatePassword(String value) {
Pattern pattern =
r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$';
RegExp regex = new RegExp(pattern);
print(value);
if (value.isEmpty) {
return 'Please enter password';
} else {
if (!regex.hasMatch(value))
return 'Enter valid password';
else
return null;
}
}
Run Code Online (Sandbox Code Playgroud)