简体   繁体   English

Django 中的 RegexValidator 模型未正确验证 email

[英]RegexValidator in Django Models not validating email correctly

I'm making a django form with an email field and using a RegexValidator and want a specific format of of the email but it seems to be not validating the field correctly我正在制作一个带有 email 字段的 django 表单,并使用 RegexValidator 并想要 email 的特定格式,但它似乎没有正确验证该字段

    email = models.EmailField(
        unique=True,
        validators=[
            RegexValidator(
                regex=r"^[2][2][a-zA-Z]{3}\d{3}@[nith.ac.in]*",
                message=
                "Only freshers with college email addresses are authorised.")
        ],
    )

When I am entering the email ending with @nith.ac.in or @nith.acin both are getting accepted while @nith.acin should not get accepted... any sols?当我输入以 @nith.ac.in 或 @nith.acin 结尾的 email 时,两者都被接受了,而 @nith.acin 不应该被接受……任何溶胶?

The [nith.ac.in] does not parse literal text, it parses any character in te group, meaning it can end with a sequence of n s, i s, ni s, etc. [nith.ac.in]解析文字文本,它解析 te 组中的任何字符,这意味着它可以以n s、 i s、 ni s 等序列结尾。

Your regex should look like:您的正则表达式应如下所示:

email = models.EmailField(
    unique=True,
    validators=[
        RegexValidator(
            regex=r'^[2][2][a-zA-Z]{3}\d{3}@nith[.]ac[.]in$',
            message='Only freshers with college email addresses are authorised.',
        )
    ],
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM