Algorithm for validating a tax identification code in PHP

Algorithm for validating a tax identification code in PHP

Online by JSC0d3
December 14, 2018 | | Recognition

Preliminary step: Testing whether the code complies with the format of a fiscal identification code (CIF). That is, the maximum length should be 10 digits and contain only numeric characters.

Validation algorithm in PHP

Step 1: Use the test key “753217532”. The digits of the code as well as the test key are reversed.
Step 2: Ignore the first digit of the reversed code (this is the control number) and multiply each digit by the corresponding digit in the reversed test key.
Step 3: Collect all the obtained products. The resulting amount is multiplied by 10 and the product is divided by 11. The figure obtained from MODULO 11 is the verification number. If the remainder has obtained the remainder 10 then the verification number will be 0.
Step 4: For a valid CIF, the verification number will have to match the original CIF control.

if ( ! function_exists( 'js_code_validate_cif' ) ) {
	/**
	 * Validate CIF number
	 *
	 * @param $cif
	 *
	 * @return bool
	 */
	function js_code_validate_cif( $cif ) {
		// If it is a string, remove the fiscal attribute and spaces
		if ( ! is_int( $cif ) ) {
			$cif = strtoupper( $cif );
			if ( strpos( $cif, 'RO' ) === 0 ) {
				$cif = substr( $cif, 2 );
			}
			$cif = (int) trim( $cif );
		}

		// if it has more than 10 digits or less than 2, it is not valid
		if ( strlen( $cif ) > 10 || strlen( $cif ) < 2 ) {
			return false;
		}
		// control numabe
		$v = 753217532;

		// extracts the control number
		$c1  = $cif % 10;
		$cif = (int) ( $cif / 10 );

		// perform operations on figures
		$t = 0;
		while( $cif > 0 ) {
			$t   += ( $cif % 10 ) * ( $v % 10 );
			$cif = (int) ( $cif / 10 );
			$v   = (int) ( $v / 10 );
		}

		// apply multiplication by 10 and learn modulo 11
		$c2 = $t * 10 % 11;

		// if modulo 11 is 10, then the control number is 0
		if ( $c2 == 10 ) {
			$c2 = 0;
		}

		return $c1 === $c2;
	}
}
JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | February 9, 2019

WordPress' wp_link_pages() function, used for displaying page links in multi-page posts, lacks one big feature You can display a list of...

Posted by | November 8, 2017

Nowadays, a lot of bloggers choose to remove HTML tag from WordPress websites However, for beginners and newbies in this field, they may feel...

Posted by | October 1, 2017

The WordPress platform is a magnet for those who want to take matters into their own hands, who want complete control over their websites and want to...

Previous PostBackNext Post

Leave here an impression