【发布时间】:2019-05-22 09:39:33
【问题描述】:
我创建了一个登录页面,我需要将这些内容添加到我的密码中。我该如何使用验证警报消息?
- 至少 1 个大写字母
- 至少 1 个小写字母
- 最少 1 个数字
- 最少 1 个特殊字符
- 通用允许字符 (!@#$ & *~)
【问题讨论】:
我创建了一个登录页面,我需要将这些内容添加到我的密码中。我该如何使用验证警报消息?
【问题讨论】:
您需要使用正则表达式来验证结构。
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
此函数将验证传递的值是否具有结构。
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
}
【讨论】:
Vignesh123! : true.. 这也是工作吗? !123viGnesh
您的正则表达式应如下所示:
r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$
这里有一个解释:
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
$
将上述表达式与您的密码字符串匹配。使用这种方法-
String? validatePassword(String value) {
RegExp regex =
RegExp(r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$');
if (value.isEmpty) {
return 'Please enter password';
} else {
if (!regex.hasMatch(value)) {
return 'Enter valid password';
} else {
return null;
}
}
}
【讨论】:
•Common Allow Character ( ! @ # $ & * ~ )
? 字符。
您必须使用带有验证器属性的 TextFormField 小部件。
TextFormField(
validator: (value) {
// add your custom validation here.
if (value.isEmpty) {
return 'Please enter some text';
}
if (value.length < 3) {
return 'Must be more than 2 charater';
}
},
),
【讨论】:
您可以使用下面的颤振插件来实现这一点。
你可以像这样使用它:
TextFormField(
decoration: InputDecoration(
labelText: 'Password',
),
validator: Validators.compose([
Validators.required('Password is required'),
Validators.patternString(r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$', 'Invalid Password')
]),
),
它的文档非常好。您可以阅读它以了解更多类似这样的实用功能。
【讨论】:
这是完整的答案
编写一个 Dart 程序来检查一个字符串是否是一个有效的密码。一个。密码必须至少有十个字符。湾。密码 仅由字母和数字组成。 C。密码必须包含在 至少两位数。
import 'dart:io';
main() {
var password;
stdout.write("Enter You'r Password: ");
password=stdin.readLineSync();
if(password.length>=10 && !password.contains(RegExp(r'\W')) && RegExp(r'\d+\w*\d+').hasMatch(password))
{
print(" \n\t$password is Valid Password");
}
else
{
print("\n\t$password is Invalid Password");
}
【讨论】:
this is the best regx
bool passValid = RegExp("^(?=.{8,32}\$)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#\$%^&*(),.?:{}|<>]).*").hasMatch(value);
if (value.isEmpty ||!passValid)
{
return 'error';
}
【讨论】: