integer validation in django not working -
forms.py
class usercreateprofileform(forms.modelform): fields = ['phone_daytime', 'phone_mobile'] def clean(self): cd=self.cleaned_data validate_integer(cd.get('phone_daytime', none)) validate_integer(cd.get('phone_mobile', none)) return cd def validate_integer(phone_daytime,phone_mobile): try: int(phone_daytime,phone_mobile) except (valueerror, typeerror): raise validationerror('phone number must number')
i want validate form 2 phone number field.
the above 1 not working,not throwing error not functioning.
the field should not accept alphabet,special character , blank allowed.how validation.
thanks
for validation of phone number approach should take.
class usercreateprofileform(forms.modelform): fields = ['phone_daytime', 'phone_mobile'] def clean(self): cd = self.cleaned_data validate_phonenumber(cd.get('phone_daytime', none)) validate_phonenumber(cd.get('phone_mobile', none)) return cd def validate_phonenumber(phone_number): char in phone_number: if not char.isdigit(): raise validationerror("phone number must number")
the error had in code tried int(phone_daytime, phone_mobile)
. not correct, throws typeerror()
. did removes leading 0
from phone number. wasn't bad since didn't use parsed number anyways know.
Comments
Post a Comment