1

I want to perform a field validation,but the conditions are

1)The field should have 10 character.

2)off these 1st 5 character should be alphabets and next 5 character should be numeric digits

I performed validation for maximum length check,but rest of the thing how to perform.Is that can be done on a single "if" condition.

I am searching for the logic in google for performing that,but not got any idea.Can any one help me to perform the same.

forms.py for length check

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

I am using this code to do but it is not working.

Thanks

0

1 Answer 1

2

Perhaps you could use something like his:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.