0

Based on this answer here: Using range in regex for Arabic letters

I want to validate a Form field to allow only Arabic characters. My Form is:

fullname_arabic = forms.CharField(
    label=_('Arabic Full Name'),
    widget=widgets.TextInput(),
    help_text=_('Please enter your arabic name'),
)

The Form has a clean method which is giving me problems to make it work. Actually it is allowing any character:

def clean_fullname_arabic(self):
    fullname_arabic = self.cleaned_data['fullname_arabic']
    if not re.match(r'[\u0627-\u064a]+$', fullname_arabic):
        raise forms.ValidationError("Only Arabic chars")

I know the problem must be in my Regex, but I am confusing concepts here and can't make it work.

Community
  • 1
  • 1
PepperoniPizza
  • 8,842
  • 9
  • 58
  • 100
  • Shouldn't you be trying with `^[\u0627-\u064a]$` ? According to [Wiki](https://en.wikipedia.org/wiki/Arabic_script_in_Unicode) there are more characters you might want to include. –  Apr 01 '16 at 00:05

1 Answers1

1

There a couple of issues with that check line, first the Regex should be reversed like (you may want to add _-):

if re.findall(u'[^\u0627-\u064a\W]', fullname_arabic, flags=re.UNICODE)

also notice r' is replaced by u', because \u0628 won't be interpreted wi r'

Edit: From a previous project of mine, the Arabic range should be changed to \u0621-\u06ED

ahmed
  • 5,430
  • 1
  • 20
  • 36