Reputation: 479
I am making a register form that can check whether there is or not in the database and how to validate the username must use alphanumeric and no spaces, this is my code for now
String validateUsername(String value) {
String pattern = r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
RegExp regExp = new RegExp(pattern);
if (value.length == 0) {
return "Username is Required";
} else if(!regExp.hasMatch(value)){
return "Invalid username";
}else {
return null;
}
}
I think the validation is almost the same as email, but I'm still confused
Upvotes: 0
Views: 1356
Reputation: 11005
This is how I do it. while using flutter.
// import needed for BlackListingTextInputFormatter function
import 'package:flutter/services.dart';
TextField(
inputFormatters: [
BlacklistingTextInputFormatter(
new RegExp("[^a-z^A-Z^0-9]+")) //Regex for accepting only alphanumeric characters
],
)
using this it will ensure no character other than alphanumeric can pass through the textfield.
so now you just need to check the string you get from that field with the database.
You can make an api and make an query like
SELECT * FROM user WHERE user_name = "userName";
if you get an empty response then that username is available.
Upvotes: 1