add username validation
This commit is contained in:
@@ -33,17 +33,36 @@ class _MyTextFieldState extends State<MyTextField> {
|
|||||||
|
|
||||||
String? get _errorText {
|
String? get _errorText {
|
||||||
final text = widget.controller.text;
|
final text = widget.controller.text;
|
||||||
|
String _errorMessage = '';
|
||||||
if (startup) {
|
if (startup) {
|
||||||
return null;
|
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;
|
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) {
|
bool isEmailValid(String email) {
|
||||||
return RegExp(r'^[\w-\.]+@[a-zA-Z]+\.[a-zA-Z]{2,}$').hasMatch(email);
|
return RegExp(r'^[\w-\.]+@[a-zA-Z]+\.[a-zA-Z]{2,}$').hasMatch(email);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:patient_manager/components/myErrorMessage.dart';
|
import 'package:patient_manager/components/myErrorMessage.dart';
|
||||||
import 'package:patient_manager/components/mySuccessMessage.dart';
|
import 'package:patient_manager/components/mySuccessMessage.dart';
|
||||||
import 'package:patient_manager/components/myTextInput.dart';
|
import 'package:patient_manager/components/myTextInput.dart';
|
||||||
@@ -27,6 +28,7 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
final fnameController = TextEditingController();
|
final fnameController = TextEditingController();
|
||||||
final lnameController = TextEditingController();
|
final lnameController = TextEditingController();
|
||||||
late bool businessUser;
|
late bool businessUser;
|
||||||
|
final FocusNode _focusNode = FocusNode();
|
||||||
|
|
||||||
bool isFieldsFilled() {
|
bool isFieldsFilled() {
|
||||||
if (fnameController.text.isEmpty ||
|
if (fnameController.text.isEmpty ||
|
||||||
@@ -52,7 +54,10 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
} else {
|
} else {
|
||||||
profileType = "personal";
|
profileType = "personal";
|
||||||
}
|
}
|
||||||
|
print("is username valid ${isUsernameValid(usernameController.text)}");
|
||||||
|
if (isUsernameValid(usernameController.text) == false) {
|
||||||
|
usernamePopUp();
|
||||||
|
} else {
|
||||||
var response = await http.put(
|
var response = await http.put(
|
||||||
Uri.parse("${AppEnviroment.baseApiUrl}/user/update/"),
|
Uri.parse("${AppEnviroment.baseApiUrl}/user/update/"),
|
||||||
headers: <String, String>{
|
headers: <String, String>{
|
||||||
@@ -78,6 +83,7 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
internetConnectionPopUp();
|
internetConnectionPopUp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool isBusinessUser() {
|
bool isBusinessUser() {
|
||||||
if (widget.signedInUser.type == "personal") {
|
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
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -122,7 +155,16 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
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: [
|
children: [
|
||||||
const Text(
|
const Text(
|
||||||
"Personal profile:",
|
"Personal profile:",
|
||||||
@@ -158,7 +200,7 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
const Text(
|
const Text(
|
||||||
"Activate Business",
|
"Activate Business Account",
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
fontSize: 20,
|
fontSize: 20,
|
||||||
@@ -187,20 +229,12 @@ class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
|
|||||||
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
|
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
|
||||||
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
|
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
if (isFieldsFilled()) {
|
submitForm();
|
||||||
updateUserApiCall();
|
|
||||||
} else {
|
|
||||||
showDialog(
|
|
||||||
context: context,
|
|
||||||
builder: (context) {
|
|
||||||
return const MyErrorMessage(errorType: "Input Error");
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user