Skip to content

Instantly share code, notes, and snippets.

@przemoc
Last active February 6, 2018 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save przemoc/571093 to your computer and use it in GitHub Desktop.
Save przemoc/571093 to your computer and use it in GitHub Desktop.
Validation of IBAN, ISBN, ISSN, UESRP, NER and VATIN numbers
# SPDX-License-Identifier: MIT
## Copyright (C) 2010 Przemyslaw Pawelczyk <przemoc@gmail.com>
##
## This script is licensed under the terms of the MIT license.
## https://opensource.org/licenses/MIT
module Validations # version 0.1
# International Bank Account Number
def iban?
unless /\A[A-Z]{2}\d{2} ?[A-Z\d]{4}( ?\d{4}){1,} ?\d{1,4}\z/ === self
return false
end
iban = self.gsub( / /, '' )
iban = ( iban[4..-1] + iban[0..3] ).gsub( /[A-Z]/ ) { |c| ( c[0] - 55 ).to_s }
iban.to_i % 97 == 1
end
# International Standard Book Number
def isbn?
unless [13,17].include?( self.size ) and /\A((978|979)-)?\d+-\d+-\d+-[X\d]\z/ === self
return false
end
isbn = self.gsub( '-', '' )
r = 0
case isbn.size
when 10
0.upto( 8 ) { |i| r += isbn[i,1].to_i * [10,9,8,7,6,5,4,3,2][i] }
r = ( 11 - r % 11 ) % 11
r = ( r < 10 ) ? r.to_s : 'X'
when 13
0.upto( 11 ) { |i| r += isbn[i,1].to_i * [1,3][i % 2] }
r = ( ( ( 10 - r ) % 10 ) % 10 ).to_s
end
isbn[-1,1] == r
end
# International Standard Serial Number
def issn?
unless self.size == 9 and /\A\d{4}\-\d{3}[X\d]\z/ === self
return false
end
issn = self.gsub( '-', '' )
r = 0
0.upto( 6 ) { |i| r += issn[i,1].to_i * [8,7,6,5,4,3,2][i] }
r = ( 11 - r % 11 ) % 11
issn[-1,1] == ( ( r < 10 ) ? r.to_s : 'X' )
end
# Polish: Powszechny Elektroniczny System Ewidencji Ludności
# Universal Electronic System for Registration of the Population
def pesel?
unless self.size == 11 and /\A\d+\z/ === self
return false
end
r = 0
0.upto( 10 ) { |i| r += self[i,1].to_i * [1,3,7,9,1,3,7,9,1,3,1][i] }
r % 10 == 0
end
# Polish: Rejestr Gospodarki Narodowej
# National Economy Register
def regon?
unless [9,14].include?( self.size ) and /\A\d+\z/ === self
return false
end
r = 0
case self.size
when 9
0.upto( 7 ) { |i| r += self[i,1].to_i * [8,9,2,3,4,5,6,7][i] }
when 14
0.upto( 12 ) { |i| r += self[i,1].to_i * [2,4,8,5,0,9,7,3,6,1,2,4,8][i] }
end
( r % 11 ) % 10 == self[-1,1].to_i
end
# Value Added Tax Identification Number
# uses VIES - VAT Information Exchange System
def vatin?
require 'net/http'
unless self.size <= 20 && /\A[A-Z]{2}/ === self
return false
end
vat = self.gsub( /[ \-]/, '' )
req = Net::HTTP::Post.new( '/taxation_customs/vies/viesquer.do' )
req.set_form_data( { 'iso' => vat[0..1], 'vat' => vat[2..-1] } )
res = Net::HTTP.start( 'ec.europa.eu' ) { |q| q.request( req ) }
res.body.include?( 'Yes, valid VAT number' )
end
end
class String
include Validations
end
@przemoc
Copy link
Author

przemoc commented Feb 6, 2018

commit 4b58f417039738016c6d9e542aaa381ee61c036c
Author: Przemyslaw Pawelczyk <przemoc@gmail.com>
Date:   2018-02-06 15:22:07 +0100

    validations.rb: Add copyright notice and MIT license notice.

commit 7f80078b4a95949123b2c8a941b18426d9ddbc46
Author: Przemyslaw Pawelczyk <przemoc@gmail.com>
Date:   2018-02-06 15:22:50 +0100

    Add SPDX License Identifier.

    The Software Package Data Exchange (SPDX) is a good initiative, it has
    matured over time and deserves accelerated adoption in open-source.

    https://spdx.org/learn
    https://spdx.org/using-spdx
    https://spdx.org/license-list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment