Django: Checking CharField format in form with a regular expression
you could also ask from both part in your form, it would be cleaner for the user :
class CapaForm(forms.Form):
capa1 = forms.IntegerField(max_value=9999, required=False)
capa2 = forms.IntegerField(max_value=99, required=False)
and then just join them in your view :
capa = self.cleaned_data.get('capa1', None) + '-' + self.cleaned_data.get('capa2', None)
First you need a regex validator: Django validators / regex validator
Then, add it into the validator list of your field: using validators in forms
Simple example below:
from django.core.validators import RegexValidator
my_validator = RegexValidator(r"A", "Your string should contain letter A in it.")
class MyForm(forms.Form):
subject = forms.CharField(
label="Test field",
required=True, # Note: validators are not run against empty fields
validators=[my_validator]
)