treetop
treetop

Reputation: 175

Specify detailed format of validation in Flask wtforms

I've created a form i'm currently validating for both DataRequired and Length.

class MainSearch(Form):
        discnumber = StringField('discnumber', validators=[DataRequired(), Length(min=7, max=7)])

I want to go one step further and validate the entry so that it matches a particular format. Specifically, I would like it to throw up an error if the user enters a disc number that doesn't begin with the letters DA followed by 5 digits.

For example, these would be examples of accepted formats:

DA26727

DA00056

DA78786

Whilst these would be unacceptable:

DA890899 (i.e. this has 6 digits rather than 5)

D989089 (This doesn't contain the 'DA' prefix)

I haven't found any comparable examples online. Is this possible with wtforms? And if s which type of validator would be most suited?

Thanks

Upvotes: 0

Views: 1485

Answers (1)

guigen
guigen

Reputation: 91

Use a Regexp validator:

discnumber = StringField('discnumber', validators=[DataRequired(), Regexp('^DA[0-9]{5}$')])

Upvotes: 1

Related Questions