1

我想执行字段验证,但条件是

1)该字段应该有10个字符。

2)前 5 个字符应该是字母,接下来的 5 个字符应该是数字

我对最大长度检查进行了验证,但其余的事情如何执行。这可以在单个“if”条件下完成。

我正在谷歌中搜索执行该操作的逻辑,但不知道。任何人都可以帮助我执行相同的操作。

forms.py 用于长度检查

def clean_bookref(self):
        cd=self.cleaned_data
        bookref=cd.get('bookref')

        if len(bookref)<10 and re.match(r'[A-z0-9]+', bookref):
            raise forms.ValidationError("Should be 10 digits")

        return bookref

我正在使用此代码来执行此操作,但它不起作用。

谢谢

4

1 回答 1

2

也许你可以使用类似他的东西:

def clean_bookref(self):
    cd=self.cleaned_data
    bookref=cd.get('bookref')

    if not re.match(r'^[A-Za-z]{5}[0-9]{5}$',bookref) :
        raise forms.ValidationError("Should be of the form abcde12345")

   return bookref
于 2013-03-18T08:49:43.057 回答