rahaf aldrwish
rahaf aldrwish

Reputation: 57

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

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

Upvotes: 1

Views: 1218

Answers (2)

mohammad esmaili
mohammad esmaili

Reputation: 1747

You can use

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

Upvotes: 1

Thierry P. Oliveira
Thierry P. Oliveira

Reputation: 626

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.

Upvotes: 0

Related Questions