If this is your first visit, check our documentation. To post, register or login.
Forums / Contribution-Writing Guidelines / Converting osc contribution Post code Validation

Converting osc contribution Post code Validation

Started by vkd1980 on 23 Aug 2012, 10:45 AM · Views: 3,157

Results 1 to 6 of 6
23 Aug 2012, 10:45 AM
#1
vkd1980 avatar

vkd1980

New Zenner

Join Date:
Jul 2012
Location:
Kochi,Kerala
Posts:
58
Plugin Contributions:
2

Converting osc contribution Post code Validation

Can any one tell me whats wrong with this function?, I am a Newbie in PHP

Function in "\includes\functions\functions_general.php" to Check

// VALIDATE POSTCODE START
function tep_validate_postcode($country_id, $postcode)
	{
	$postval =$db->Execute( "select countries_postal_validation from countries where countries_id = '" . (int)$country_id . "' limit 1");
	
		if (zen_not_null($postval->fields['countries_postal_validation']))
		{
		return (preg_match($postval->fields['countries_postal_validation'], $postcode));
		}
	else return true;
    }
// VALIDATE POSTCODE END

Checking of entry in "\includes\modules\pages\address_book_process\header_php.php"

if (strlen($postcode) < ENTRY_POSTCODE_MIN_LENGTH) {
    $error = true;
    $messageStack->add('addressbook', ENTRY_POST_CODE_ERROR);
  }
// VALIDATE POSTCODE START
	if (tep_validate_postcode($country, $postcode) == false)
		{
		$error = true;
		$messageStack->add('addressbook', ENTRY_POST_CODE_VALID_ERROR);
		}
// VALIDATE POSTCODE END

Thanks in Advance

Vinod

23 Aug 2012, 11:10 AM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: Converting osc contribution Post code Validation

You can't access the $db variable from inside a function if you don't declare it as a global first.

function name_of_function()
  {
    global $db;
    // rest of code which uses $db goes here
  }

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

23 Aug 2012, 11:50 AM
#3
vkd1980 avatar

vkd1980

New Zenner

Join Date:
Jul 2012
Location:
Kochi,Kerala
Posts:
58
Plugin Contributions:
2

Re: Converting osc contribution Post code Validation

DrByte:

You can't access the $db variable from inside a function if you don't declare it as a global first.

function name_of_function()
{
global $db;
// rest of code which uses $db goes here
}


Thank you DrByte,

gotcha, I just forgot:bangin:

Uploading the contribution, will be available to download after authorization
25 Aug 2012, 1:46 AM
#4
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Converting osc contribution Post code Validation

vkd1980:

Can any one tell me whats wrong with this function?, I am a Newbie in PHP

I note that DrByte has provided the solution, but I do have another very minor suggestion of my own.

vkd1980:

function tep_validate_postcode($country_id, $postcode)

My suggestion is to change "tep_validate_postcode" to "zen_validate_postcode" (in every place it is referenced).

Although this will make NO DIFFERENCE to functionality, the change will serve several purposes.
Firstly it will make the code follow the zencart coding standards.
Secondly it will obfuscate the fact that this was a direct port from an OsCommerce module
Thirdly, in the odd chance you ever need to perform a global search for zencart functions using the 'zen_' prefix it will assure that your function will be found along with all the other zen_ functions.

As I say, it's not a big deal, and it's not worth rushing out to make the change. It's just something to keep in mind and possibly implement if/when you need to make other changes.

Cheers
Rod

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

25 Aug 2012, 2:28 AM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: Converting osc contribution Post code Validation

or better yet, maybe "vkd_" would be better than "zen_" since it's not really a Zen Cart function.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

25 Aug 2012, 4:59 AM
#6
vkd1980 avatar

vkd1980

New Zenner

Join Date:
Jul 2012
Location:
Kochi,Kerala
Posts:
58
Plugin Contributions:
2

Re: Converting osc contribution Post code Validation

DrByte:

or better yet, maybe "vkd_" would be better than "zen_" since it's not really a Zen Cart function.

RodG:

My suggestion is to change "tep_validate_postcode" to "zen_validate_postcode" (in every place it is referenced).

Me Also Agree,

I will Change the function Name to "zen_validate_postcode" and will upload fresh file soon,

Dear Moderator,

Please reject the current file uploaded

Vinod