from django import forms
class ActonForm(forms.Form):
creator = forms.RegexField('^[a-zA-Z0-9\-' ]$',max_length=30, min_length=3)
data = {'creator': 'hello'
}
f = ActonForm(data)
print f.is_valid()
Why doesn’t this work? have i made a wrong regular expression? I wanted a name field with provision for single quotes and a hyphen
It kind of shows in the syntax highlighting. The apostrophe in the regex isn’t escaped, it should be like this:
Edit: When escaping things in the regular expression, you need double backslashes. I doubled the backslash before the hyphen (not that it has to be escaped in this particular case.)
Secondly, your regular expression only allows for a single character. You need to use a quantifier. + means one or more, * means 0 or more, {2,} means two or more, {3,6} means three to six. You probably want this:
Do take care that the above regular expression will allow spaces in the start and end of the field as well. To avoid that you need a more complex regex.