简体   繁体   中英

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 = 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?

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.

Your regex should look like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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