這個問題在這里已經有了答案: 正則運算式驗證密碼強度 11 個答案 3天前關閉。
我正在我的應用程式中制作登錄表單。這是我的密碼強度標準如下:
- 8 個字符長度
- 2個大寫字母
- 1 個特殊字符 (!@#$&*)
- 2 個數字 (0-9)
- 3個小寫字母
uj5u.com熱心網友回復:
您需要使用正則運算式來驗證結構。
bool validateStructure(String value){
String pattern = r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$';
RegExp regExp = new RegExp(pattern);
return regExp.hasMatch(value);
}
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
}
uj5u.com熱心網友回復:
you can form_field_validator plugin and you can change it accordingly to your requirements. Hope this will work for you, Thanks
link of the plugin. https://pub.dev/packages/form_field_validator
import 'package:form_field_validator/form_field_validator.dart';
TextFormField(
controller: passwordController,
validator: MultiValidator([
RequiredValidator(errorText: "* Required"),
MinLengthValidator(6,
errorText:
"Password should be atleast 6 characters"),
PatternValidator(r'(?=.*?[#?!@$%^&*-])',
errorText:
'passwords must have at least one special character'),
MaxLengthValidator(15,
errorText:
"Password should not be greater than 15 characters")
]),
obscureText: Provider.of<LoginProvider>(context,
listen: false)
.isTrue,
keyboardType: TextInputType.emailAddress,
autofillHints: [AutofillHints.email],
cursorColor: AppColors.black,
style: TextStyle(
color: Color(0xff919AAA),),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: AppColors.textFieldColor,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: AppColors.textFieldColor,
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: AppColors.red,
),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: AppColors.red,
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: AppColors.textFieldColor,
),
),
floatingLabelBehavior:
FloatingLabelBehavior.never,
suffixIcon: IconButton(
onPressed: () {
Provider.of<LoginProvider>(context,
listen: false)
.toggleObs();
},
icon: Provider.of<LoginProvider>(context,
listen: false)
.switchObsIcon,
),
labelText: 'Password',
errorStyle: TextStyle(color: AppColors.red),
hintStyle: TextStyle(
color: Color(0xff919AAA),
),
),
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420038.html
標籤:
