Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django RegexField Letters and Numbers only

I need a regexfield in a django form to only accept letters and numbers, and nothing else. I've tried this, but it didn't work:

myfield = forms.RegexField(label=("My Label"), max_length=31, regex=r'[a-zA-Z0-9]',
        error_message = ("This value must contain only letters and numbers."),

I am not very good at using regular expressions. Can you tell me what I am doing wrong here and how to fix it?

like image 295
imns Avatar asked Sep 30 '10 00:09

imns


People also ask

How do you make a model field required in Django?

Let's try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field. Hit submit. Hence Field is accepting the form even without any data in the geeks_field. This makes required=False implemented successfully.

How do you remove this field is required Django?

If yes try to disable this behavior, set the novalidate attribute on the form tag As <form action="{% url 'new_page' %}", method="POST" novalidate> in your html file.

How to add validation in Django?

Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data. The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

How does Django validate email?

Email-Verification for Django. Email verification for new signups or new users is a two-step verification process and adds a layer for security for valid users. verify_email is a django app that provides this functionality right of the bat without any complex implementation.


1 Answers

regex=r'[a-zA-Z0-9]',

Is one letter.

Do you want more than one letter? Then use a repeat

regex=r'[a-zA-Z0-9]+',

There are numerous tutorials on regular expressions. Please google for "Regular Expression Tutorial."

like image 74
S.Lott Avatar answered Nov 01 '22 09:11

S.Lott