Flutter Textformfield:在TextFormField输入中需要一个特殊字符

发布于 2025-02-11 10:10:31 字数 1556 浏览 2 评论 0原文

我正在使用Flutter中的TextFormfield输入登记页面。

我想要的是,如果没有char'@'和'。然后它将被拒绝。

这是我希望的结果:

输入:name

errortext:“您应该输入有效的电子邮件地址”

输入: [电子邮件  prected]

响应:成功

,这是我的代码:

                      TextFormField(
                          keyboardType: TextInputType.emailAddress,
                          inputFormatters: [
                            FilteringTextInputFormatter.allow(
                                RegExp("[a-z0-9@._-]")),

                          ],
                          onChanged: (text) {
                            _onSearchChanged(text);
                          },
                          controller: emailController,
                          decoration: InputDecoration(
                              errorText:
                              isEmailInvalid ? "Email is already taken" : null,
                              hintText: 'Enter your email',
                              suffixIcon: isEmailvalid
                                  ? const Icon(
                                Icons.check,
                                color: Colors.green,
                              )
                                  : const Icon(
                                  Icons.check_circle,
                                  color: Colors.transparent)
                          )),

I'm using TextFormField in Flutter to input email for register page.

What I want is if there's no char '@' and '.' then it will get rejected.

This is the result I hope for :

input : name

errorText : "You should enter valid email address"

input : [email protected]

response : success

And this is my code :

                      TextFormField(
                          keyboardType: TextInputType.emailAddress,
                          inputFormatters: [
                            FilteringTextInputFormatter.allow(
                                RegExp("[a-z0-9@._-]")),

                          ],
                          onChanged: (text) {
                            _onSearchChanged(text);
                          },
                          controller: emailController,
                          decoration: InputDecoration(
                              errorText:
                              isEmailInvalid ? "Email is already taken" : null,
                              hintText: 'Enter your email',
                              suffixIcon: isEmailvalid
                                  ? const Icon(
                                Icons.check,
                                color: Colors.green,
                              )
                                  : const Icon(
                                  Icons.check_circle,
                                  color: Colors.transparent)
                          )),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2025-02-18 10:10:31

您可以使用它来验证电子邮件,告诉我是否有任何问题

  bool isEmail = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(your_email_here);

You can use it to validate email, tell me if have any problem

  bool isEmail = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(your_email_here);
寻找一个思念的角度 2025-02-18 10:10:31

您需要将TextFieldForm包裹在表单中,并为其设置验证器。您可以参考此示例代码:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

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

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  @override
  Widget build(BuildContext context) {
    return Material(
      child: Center(
        child: Form(
              autovalidateMode: AutovalidateMode.always,
              onChanged: () {
                Form.of(primaryFocus!.context!)!.save();
              },
              child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: ConstrainedBox(
                      constraints: BoxConstraints.tight(const Size(200, 50)),
                      child: TextFormField(
                        validator: (String? value) {
    return value != null && RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(value) ? null : 'Invalid email';
  }
                      ),
                    ),
                  ),
            ),
          ),
    );
  }
}

You need to wrap your TextFieldForm inside a Form and set the validator for it. You can refer to this example code:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

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

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  @override
  Widget build(BuildContext context) {
    return Material(
      child: Center(
        child: Form(
              autovalidateMode: AutovalidateMode.always,
              onChanged: () {
                Form.of(primaryFocus!.context!)!.save();
              },
              child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: ConstrainedBox(
                      constraints: BoxConstraints.tight(const Size(200, 50)),
                      child: TextFormField(
                        validator: (String? value) {
    return value != null && RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(value) ? null : 'Invalid email';
  }
                      ),
                    ),
                  ),
            ),
          ),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文