how to validate that my text fields has characters [a-z]?
TextFormField(
maxLines: 20,
maxLength: 200,
controller: _descriptionController2,
decoration: InputDecoration(
contentPadding:
EdgeInsets.only(left: 10.0, top: 16.0),
hintText: "What do you think about the place?",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16.0)),
),
),
CodePudding user response:
You can use regex on TextFormField validator:
TextFormField(
validator: (value) {
final RegExp regex = RegExp('[a-zA-Z]');
if (value == null || value.isEmpty || !regex.hasMatch(value)) {
return 'Must have letters';
}
return null;
},
),
This regex will check if have at least one letter on your string.
CodePudding user response:
TextFormField(
maxLines: 20,
maxLength: 200,
controller: _descriptionController2,
valdator: (value) {
RegExp regEx = RegExp(r'^[A-Za-z0-9_.] $');
if(!regEx.hasMatch(value)){
return "Must have alphabets"
}
if (value!.isEmpty) {
return "Field cannot be empty";
}
},
decoration: InputDecoration(
contentPadding:
EdgeInsets.only(left: 10.0, top: 16.0),
hintText: "What do you think about the place?",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16.0)),
),
),