3

I am wondering if I can add a requirment to the setup of my User document to check for a specific string. The idea is when a User Document is created with an email address, I want to make sure the email is from a college so it should end in ".edu" example: "john.doe@college.edu" is acceptable but "john.doe@gmail.com" is not

Here is my code:

class User(db.Document, UserMixin):
    name = db.StringField(max_length=255, unique=True)
    email = db.StringField(max_length=255, unique=True)
    phone = db.StringField(max_length=255, unique=True)
    password = db.StringField(max_length=255)
    active = db.BooleanField(default=True)
    confirmed_at = db.DateTimeField()
    roles = db.ListField(db.ReferenceField(Role), default=[])
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Louis B
  • 342
  • 1
  • 5
  • 21

1 Answers1

4

There is an optional regex argument you can define:

email = db.StringField(max_length=255, unique=True, regex=r'.*?boston\.edu$')

And, also, why not use a specific EmailField in this case:

email = db.EmailField(max_length=255, unique=True, regex=r'.*?boston\.edu$')
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • i believe this makes it so the whole field must equal .edu however i just want to the field to contain .edu – Louis B Jan 10 '15 at 22:33
  • sorry I am very unfarmiliar with regex but how could i alter the code to make it require "boston.edu"? – Louis B Jan 10 '15 at 23:28
  • Thank you for everything but 1 more question: what if I want only 2 colleges now so boston.edu and mass.edu? is there a way to put an or statement? – Louis B Jan 11 '15 at 03:05
  • 2
    @LouisB sure: `.*?(boston|mass)\.edu$` – alecxe Jan 11 '15 at 03:51