Home > Software engineering >  RegExp not working with the Validation Form in flutter?
RegExp not working with the Validation Form in flutter?

Time:10-18

This is a part of a project regarding feedback forms.

I already manage to create the validation form properly thanks to the answers here in StackOverflow developers.

But the problem is creating this regular expression which seems to not work on the validation form. I found in flutter docs the Iterable but I do not know how can I implement it on the dart page.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class SimpleDialog extends StatelessWidget {
  // ignore: prefer_typing_uninitialized_variables
  final title;
  const SimpleDialog(this.title, {super.key});

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('Alert'),
      content: Text(title),
      actions: [
        TextButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: const Text('OK'))
      ],
    );
  }
}

class FeedbackPage extends StatefulWidget {
  const FeedbackPage({super.key});

  @override
  State<FeedbackPage> createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {
  final nameOfuser = TextEditingController();
  final emailOfuser = TextEditingController();
  final messageOfuser = TextEditingController();
  final _formKey = GlobalKey<FormState>();

  List<bool> isTypeSelected = [false, false, false, true, true];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // AppBar para sa taas ng design
        appBar: AppBar(
          centerTitle: true,
          title: const Text(
            "PicLeaf",
            style: TextStyle(
                color: Color.fromRGBO(102, 204, 102, 1.0),
                fontWeight: FontWeight.bold),
          ),
          backgroundColor: Colors.white,
          shadowColor: const Color.fromARGB(255, 95, 94, 94),
        ),

        //body of the application
        backgroundColor: const Color(0xffeeeeee),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              const SizedBox(
                height: 20,
              ),
              const Text(
                "Feedback",
                style: TextStyle(
                    fontSize: 30.0,
                    fontFamily: 'RobotoBold',
                    fontWeight: FontWeight.bold,
                    color: Color.fromRGBO(102, 204, 102, 1.0)),
              ),
              const Text(
                "Give us your feedback!",
                style: TextStyle(
                  fontSize: 18.0,
                  fontFamily: 'RobotoMedium',
                ),
              ),
              const SizedBox(
                height: 20,
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 32.0),
                child: Form(
                  key: _formKey,
                  child: Column(
                    children: <Widget>[
                      const SizedBox(height: 16.0),
                      TextFormField(
                        controller: nameOfuser,
                        decoration: const InputDecoration(
                          filled: true,
                          fillColor: Colors.white,
                          hintText: "Name",
                          border: OutlineInputBorder(),
                        ),
                        validator: (nameOfuser) {
                          if (nameOfuser == null || nameOfuser.isEmpty) {
                            return 'Please enter your Name';
                          }
                          return null;
                        },
                      ),
                      const SizedBox(height: 8.0),
                      TextFormField(
                        controller: emailOfuser,
                        decoration: const InputDecoration(
                          filled: true,
                          fillColor: Colors.white,
                          hintText: "Email",
                          border: OutlineInputBorder(),
                        ),
                        validator: (emailOfuser) {
                          if (emailOfuser == null || emailOfuser.isEmpty) {
                            String emailOfuser1 = emailOfuser.toString();
                            String pattern = r'\w @\w \.\w ';
                            if (RegExp(pattern).hasMatch(emailOfuser1)) {
                              return 'Please enter your Email Properly';
                            } else {
                              return 'Please enter your Email';
                            }
                          }
                          return null;
                        },
                      ),
                      const SizedBox(height: 8.0),
                      TextFormField(
                        controller: messageOfuser,
                        maxLines: 6,
                        decoration: const InputDecoration(
                          filled: true,
                          fillColor: Colors.white,
                          hintText: "Message",
                          border: OutlineInputBorder(),
                        ),
                        validator: (messageOfuser) {
                          if (messageOfuser == null || messageOfuser.isEmpty) {
                            return 'Please enter your Message';
                          }
                          return null;
                        },
                      ),
                      const SizedBox(height: 8.0),
                      MaterialButton(
                        height: 50.0,
                        minWidth: double.infinity,
                        color: const Color.fromRGBO(102, 204, 102, 1.0),
                        onPressed: () {
                          if (_formKey.currentState!.validate()) {
                            showDialog(
                                context: context,
                                builder: (BuildContext context) {
                                  return const SimpleDialog(
                                      'Feedback Submitted');
                                });
                            Map<String, dynamic> data = {
                              "Name": nameOfuser.text,
                              "Email": emailOfuser.text,
                              "Message": messageOfuser.text,
                              "Time": FieldValue.serverTimestamp(),
                            };
                            setState(() {
                              nameOfuser.clear();
                              emailOfuser.clear();
                              messageOfuser.clear();
                            });
                            FirebaseFirestore.instance
                                .collection("FeedbackMessages")
                                .add(data);
                          }
                        },
                        child: const Text(
                          "SUBMIT",
                          style: TextStyle(
                            fontFamily: 'RobotoBold',
                            fontWeight: FontWeight.bold,
                            color: Colors.white,
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      Container(
                        padding: const EdgeInsets.fromLTRB(20, 20, 20, 10),
                        child: const Text(
                          'Contact Us!',
                          style: TextStyle(
                              fontSize: 30,
                              fontFamily: 'RobotoBold',
                              color: Color.fromRGBO(102, 204, 102, 1.0)),
                          textAlign: TextAlign.center,
                        ),
                      ),
                      Column(
                        children: <Widget>[
                          Container(
                            padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
                            margin: const EdgeInsets.symmetric(horizontal: 0),
                            child: TextButton.icon(
                              // <-- TextButton
                              onPressed: () {},
                              icon: const Icon(
                                Icons.facebook,
                                color: Colors.black,
                                size: 35.0,
                              ),
                              label: const Text(
                                'facebook.com/picleaf',
                                style: TextStyle(fontFamily: 'RobotoMedium'),
                              ),
                              style: TextButton.styleFrom(
                                foregroundColor: Colors.black,
                              ),
                            ),
                          ),
                          Container(
                              padding: const EdgeInsets.fromLTRB(20, 0, 20, 10),
                              margin: const EdgeInsets.symmetric(horizontal: 0),
                              child: TextButton.icon(
                                // <-- TextButton
                                onPressed: () {},
                                icon: const Icon(
                                  Icons.email,
                                  color: Colors.black,
                                  size: 35.0,
                                ),

                                label: const Text(
                                  '[email protected]',
                                  style: TextStyle(fontFamily: 'RobotoMedium'),
                                ),

                                style: TextButton.styleFrom(
                                  foregroundColor: Colors.black,
                                ),
                              )),
                        ],
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ));
  }
}

CodePudding user response:

You can use this validator:

validator: (emailOfuser) {
    if (emailOfuser == null || emailOfuser.isEmpty) {
        return 'Please enter your Email';
    } else if (emailOfuser.isNotEmpty) {
        String emailOfuser1 = emailOfuser.toString();
        String pattern = r'\w @\w \.\w ';
        if (RegExp(pattern).hasMatch(emailOfuser1) == false) {
             return 'Please enter your Email Properly';
        }
    }
    return null;
},

CodePudding user response:

try to implement like this:

import 'package:flutter/material.dart';

class EmailValidationForm extends StatefulWidget {
  const EmailValidationForm({Key? key}) : super(key: key);

  @override
  State<EmailValidationForm> createState() => _EmailValidationFormState();
}

class _EmailValidationFormState extends State<EmailValidationForm> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Email Validation'),
      ),
      body: Form(
        key: formKey,
        child: Column(
          children: [
            TextFormField(
              autofocus: false,
              maxLength: 300,
              keyboardType: TextInputType.emailAddress,
              autocorrect: false,
              validator: (email) {
                if (email!.isEmpty) {
                  return 'required field';
                } else if (email.trim().contains(' ')) {
                  return 'contain space';
                } else if (!emailValid(email)) {
                  return 'invalid email';
                }
                return null;
              },
              onSaved: (email) {
                return debugPrint(email);
              },
            ),
            ElevatedButton(
                onPressed: () {
                  if (formKey.currentState!.validate()) {
                    formKey.currentState!.save();
                  }
                },
                child: const Text('Validate')),
          ],
        ),
      ),
    );
  }
}

bool emailValid(String email) {
  final RegExp regex = RegExp(
      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,}))$");
  return regex.hasMatch(email.trim());
}

  • Related