def __call__(self, value): """ Validates the IBAN value using the official IBAN validation algorithm. https://en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN """ if value is None: return value value = value.upper().replace(' ', '').replace('-', '') # Check that the total IBAN length is correct as per the country. If not, the IBAN is invalid. country_code = value[:2] if country_code in self.validation_countries: if self.validation_countries[country_code] != len(value): msg_params = { 'country_code': country_code, 'number': self.validation_countries[country_code] } raise ValidationError( _('%(country_code)s IBANs must contain %(number)s characters.' ) % msg_params) else: raise ValidationError( _('%s is not a valid country code for IBAN.') % country_code) if self.include_countries and country_code not in self.include_countries: raise ValidationError( _('%s IBANs are not allowed in this field.') % country_code) if self.iban_checksum(value) != value[2:4]: raise ValidationError(_('Not a valid IBAN.'))
def __call__(self, value): if value is None: return value value = value.upper() # Length is 8 or 11. bic_length = len(value) if bic_length != 8 and bic_length != 11: raise ValidationError( _('BIC codes have either 8 or 11 characters.')) # First 4 letters are A - Z. institution_code = value[:4] for x in institution_code: if x not in string.ascii_uppercase: raise ValidationError( _('%s is not a valid institution code.') % institution_code) # Letters 5 and 6 consist of an ISO 3166-1 alpha-2 country code. country_code = value[4:6] if country_code not in ISO_3166_1_ALPHA2_COUNTRY_CODES: raise ValidationError( _('%s is not a valid country code.') % country_code)
def clean(self, value): value = super(TRPostalCodeField, self).clean(value) if value in self.empty_values: return self.empty_value if len(value) != 5: raise ValidationError(self.error_messages['invalid']) province_code = int(value[:2]) if province_code == 0 or province_code > 81: raise ValidationError(self.error_messages['invalid']) return value
def __call__(self, value): country_code, number = self.clean(value) try: match = re.match(VATIN_PATTERN_MAP[country_code], value) if not match: raise ValidationError(self.messages['vatin'], code='vatin', params={'vatin': value}) except KeyError: raise ValidationError(self.messages['country_code'], code='country_code', params={'country_code': country_code})
def clean(self, value): super(HRPostalCodeField, self).clean(value) if value in EMPTY_VALUES: return self.empty_value value = value.strip() if not postal_code_re.search(value): raise ValidationError(self.error_messages['invalid']) # Make sure the number is in valid range. if not 9999 < int(value) < 60000: raise ValidationError(self.error_messages['invalid']) return '%s' % value
def __call__(self, value): if value is None: return value if self.strip_nondigits: value = re.compile(r'[^\d]+').sub('', value) if not checksums.ean(value): raise ValidationError(self.message, code='invalid')
def clean(self, value): super(IDPostalCodeField, self).clean(value) if value in EMPTY_VALUES: return self.empty_value value = value.strip() if not postcode_re.search(value): raise ValidationError(self.error_messages['invalid']) if int(value) < 10110: raise ValidationError(self.error_messages['invalid']) # 1xxx0 if value[0] == '1' and value[4] != '0': raise ValidationError(self.error_messages['invalid']) return '%s' % (value, )
def clean(self, value): value = super(ARPostalCodeField, self).clean(value) if value in self.empty_values: return self.empty_value if len(value) not in (4, 8): raise ValidationError(self.error_messages['invalid']) if len(value) == 8: return '%s%s%s' % (value[0].upper(), value[1:5], value[5:].upper()) return value
def clean(self, value): super(CUProvinceField, self).clean(value) if value in self.empty_values: return self.empty_value try: return PROVINCE_NORMALIZED[value.strip().lower()] except KeyError: pass raise ValidationError(self.error_messages['invalid'])
def clean(self, value): value = super(LVPostalCodeField, self).clean(value) if value in EMPTY_VALUES: return self.empty_value match = re.match(zipcode, value) if not match: raise ValidationError(self.error_messages['invalid']) return 'LV-' + match.group('code')
def clean(self, value): value = super(GBPostalCodeField, self).clean(value) if value in self.empty_values: return self.empty_value postcode = value.upper().strip() # Put a single space before the incode (second part). postcode = self.space_regex.sub(r' \1', postcode) if not self.postcode_regex.search(postcode): raise ValidationError(self.error_messages['invalid']) return postcode
def clean(self, value): value = super(BRStateChoiceField, self).clean(value) if value in EMPTY_VALUES: value = '' value = str(value) if value == '': return value valid_values = set([str(entry[0]) for entry in self.choices]) if value not in valid_values: raise ValidationError(self.error_messages['invalid']) return value
def clean(self, value): value = super(INStateField, self).clean(value) if value in EMPTY_VALUES: return self.empty_value try: value = value.strip().lower() except AttributeError: pass else: try: return str(STATES_NORMALIZED[value.strip().lower()]) except KeyError: pass raise ValidationError(self.error_messages['invalid'])
def clean(self, value): value = value.upper() value = super(NLPostalCodeField, self).clean(value) if int(value[:4]) < 1000: raise ValidationError(self.error_messages['invalid']) if isinstance(value, six.string_types): value = value.upper().replace(' ', '') if len(value) == 6: value = '%s %s' % (value[:4], value[4:]) return super(NLPostalCodeField, self).clean(value)
def iban_checksum(value): """ Returns check digits for an input IBAN number. Original checksum in input value is ignored. """ # 1. Move the two initial characters to the end of the string, replacing checksum for '00' value = value[4:] + value[:2] + '00' # 2. Replace each letter in the string with two digits, thereby expanding the string, where # A = 10, B = 11, ..., Z = 35. value_digits = '' for x in value: if '0' <= x <= '9': value_digits += x elif 'A' <= x <= 'Z': value_digits += str(ord(x) - 55) else: raise ValidationError( _('%s is not a valid character for IBAN.') % x) # 3. The remainder of the number above when divided by 97 is then subtracted from 98. return '%02d' % (98 - int(value_digits) % 97)