add username validation
This commit is contained in:
@@ -33,17 +33,36 @@ class _MyTextFieldState extends State<MyTextField> {
|
||||
|
||||
String? get _errorText {
|
||||
final text = widget.controller.text;
|
||||
String _errorMessage = '';
|
||||
if (startup) {
|
||||
return null;
|
||||
} else if (!widget.required) {
|
||||
return null;
|
||||
} else if (text.isEmpty) {
|
||||
return "${widget.hintText} is required";
|
||||
} else if (widget.hintText == "Email" && !isEmailValid(text)) {
|
||||
return "Enter a valid email address";
|
||||
}
|
||||
if (!widget.required) {
|
||||
return null;
|
||||
}
|
||||
if (text.isEmpty) {
|
||||
return "${widget.hintText} is required";
|
||||
}
|
||||
if (widget.hintText == "Email" && !isEmailValid(text)) {
|
||||
_errorMessage += "Enter a valid email address\n";
|
||||
}
|
||||
if (widget.hintText == "Username" && text.length < 8) {
|
||||
_errorMessage += "• Username must contain at least 8 characters.\n";
|
||||
}
|
||||
if (widget.hintText == "Username" && !isUsernameValid(text)) {
|
||||
_errorMessage += "• Username can only contain '_' special Chracters.\n";
|
||||
}
|
||||
if (_errorMessage.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
// If there are no error messages, the password is valid
|
||||
return _errorMessage;
|
||||
}
|
||||
|
||||
bool isUsernameValid(String Username) {
|
||||
return RegExp(r'^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$')
|
||||
.hasMatch(Username);
|
||||
}
|
||||
|
||||
bool isEmailValid(String email) {
|
||||
return RegExp(r'^[\w-\.]+@[a-zA-Z]+\.[a-zA-Z]{2,}$').hasMatch(email);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:patient_manager/components/myErrorMessage.dart';
|
||||
import 'package:patient_manager/components/mySuccessMessage.dart';
|
||||
import 'package:patient_manager/components/myTextInput.dart';
|
||||
@@ -27,6 +28,7 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
||||
final fnameController = TextEditingController();
|
||||
final lnameController = TextEditingController();
|
||||
late bool businessUser;
|
||||
final FocusNode _focusNode = FocusNode();
|
||||
|
||||
bool isFieldsFilled() {
|
||||
if (fnameController.text.isEmpty ||
|
||||
@@ -52,7 +54,10 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
||||
} else {
|
||||
profileType = "personal";
|
||||
}
|
||||
|
||||
print("is username valid ${isUsernameValid(usernameController.text)}");
|
||||
if (isUsernameValid(usernameController.text) == false) {
|
||||
usernamePopUp();
|
||||
} else {
|
||||
var response = await http.put(
|
||||
Uri.parse("${AppEnviroment.baseApiUrl}/user/update/"),
|
||||
headers: <String, String>{
|
||||
@@ -78,6 +83,7 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
||||
internetConnectionPopUp();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isBusinessUser() {
|
||||
if (widget.signedInUser.type == "personal") {
|
||||
@@ -108,6 +114,33 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
||||
);
|
||||
}
|
||||
|
||||
void usernamePopUp() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return const MyErrorMessage(errorType: "Invalid Username");
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
bool isUsernameValid(String username) {
|
||||
return RegExp(r'^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$')
|
||||
.hasMatch(username);
|
||||
}
|
||||
|
||||
void submitForm() {
|
||||
if (isFieldsFilled()) {
|
||||
updateUserApiCall();
|
||||
} else {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return const MyErrorMessage(errorType: "Input Error");
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
setState(() {
|
||||
@@ -122,7 +155,16 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
return KeyboardListener(
|
||||
focusNode: _focusNode,
|
||||
autofocus: true,
|
||||
onKeyEvent: (event) async {
|
||||
if (event is KeyDownEvent &&
|
||||
event.logicalKey == LogicalKeyboardKey.enter) {
|
||||
submitForm();
|
||||
}
|
||||
},
|
||||
child: Column(
|
||||
children: [
|
||||
const Text(
|
||||
"Personal profile:",
|
||||
@@ -158,7 +200,7 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
const Text(
|
||||
"Activate Business",
|
||||
"Activate Business Account",
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 20,
|
||||
@@ -187,20 +229,12 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
||||
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
|
||||
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
|
||||
onTap: () {
|
||||
if (isFieldsFilled()) {
|
||||
updateUserApiCall();
|
||||
} else {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return const MyErrorMessage(errorType: "Input Error");
|
||||
},
|
||||
);
|
||||
}
|
||||
submitForm();
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user