How to make sure that my text field doesn't have only numbers, it should have letters in flutter

Viewed 38

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)),
                    ),
                  ),
2 Answers

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.

You can use

inputFormatters: <TextInputFormatter>[
       FilteringTextInputFormatter.allow(RegExp('[a-z]'))
],
Related