News:

Looking for documentation? Take a look on our wiki

Main Menu

validate UK zip

Started by AH, May 24, 2013, 21:57:27 PM

Previous topic - Next topic

AH

Milbo

I have been strugglling for days trying to do a test on the zip code for uk

Any help or direction is greatly appreciated, I know you are extremely busy

I sorted it in Vm 1.1 but and have no success in VM2 (pathetic I know  :'( )




Vm 1.1
[b]ps_userfield.php[/b]
if( isset( $required_fields['zip'] )) {
echo '
if( form.country.value == "GBR"  && !(/^([a-zA-Z]{1,2}[0-9rR][0-9a-zA-Z]? [0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2})$/.test(form.zip.value))) {
alert( \'Please enter a valid UK post code - e.g. KA27 8AA \');
return false;
}';
}

   




I can see the use of validator in edit_address.php (but am failing to get i test to work!)




<script language="javascript">
function myValidator(f, t) {
f.task.value = t; //this is a method to set the task of the form on the fTask.
if (document.formvalidator.isValid(f)) {
f.submit();
return true;
} else {
var msg = '<?php echo addslashes (JText::('COM_VIRTUEMART_USER_FORM_MISSING_REQUIRED_JS')); ?>';
alert(msg + ' ');
}
return false;
}

function callValidatorForRegister(f) {

var elem = jQuery('#username_field');
elem.attr('class', "required");

var elem = jQuery('#name_field');
elem.attr('class', "required");

var elem = jQuery('#password_field');
elem.attr('class', "required");

var elem = jQuery('#password2_field');
elem.attr('class', "required");

var elem = jQuery('#userForm');

return myValidator(f, '<?php echo $rtask ?>');

}
</script>
Regards
A

Joomla 4.4.5
php 8.1

Milbo

You must add it in our js formvalidator somewhere and in the userfield itself.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

AH

#2
Sorted, thanks Milbo, I was in the right area - just doing the wrong things!

I just added a new CASE: in admin  convirtuemart/models/userfields.php

and the created an override for edit_address.php  and added the JS there.

left formvalidator alone as no need to mod.
Regards
A

Joomla 4.4.5
php 8.1

Milbo

it would be nice to have that in the core, can you please create a patch for it? http://dev.virtuemart.net/projects/virtuemart/wiki/Setting_up_a_Development_Environment
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

AH

milbo

Am very happy to let you have the code

But I am not really a coder so it may not be up to your standards

One issue is that I still get 2 prompts when the UK postcode is not entered correctly and the country is hard coded (via the UK id)

Email addresses are not being validated as it looks like the "case" in userfields.php has been commented out, any ideas why this was done??
Regards
A

Joomla 4.4.5
php 8.1

Milbo

Things I may do then. If you provide a patch with your knowledge
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

AH

How do you want the code?

Regards
A

Joomla 4.4.5
php 8.1

Milbo

Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

AH

Milbo

Luckily you said "at best".

So my alternative is to give you the code, as to try get my head around all of the developer page information at this point, for a simple edit to the front end view seems like overkill.

View = edit_address.php

Adds a new Script (add new handler into the existing formvalidator) and , adjust the existing function myValidator script

Allowing the new UKzip handler to check "zip" field on BT and ST forms when Country is UK (222)

Errors throw a java message to suggest a correct format.  (Hard code but works for UK only anyhow!)


<script language="javascript">
//quorvia add validator element for uk zip test
window.addEvent('domready',
function(){
document.formvalidator.setHandler('UKzip',
function(value) {
var regex=/^([a-zA-Z]{1,2}[0-9R][0-9a-zA-Z]? [0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2})$/;
if (regex.test(value)) {
return true;
} else {
var msg = 'Please enter a valid UK post code - e.g. KA27 8AA';
alert (msg);
return false;
}
}
);
}
);
//quorvia end
</script>


<script language="javascript">
function myValidator(f, t) {

//quorvia new checks for UK postcode format on virtuemart_country_id and shipto_virtuemart_country_id
var country = "";
var shipto_country = "";
var country = jQuery('#virtuemart_country_id').val();
var shipto_country = jQuery('#shipto_virtuemart_country_id').val();

if (country !="") {
if (country == '222') {
var elem = jQuery('#zip_field');
elem.attr('class', "validate-UKzip");
} else {
var elem = jQuery('#zip_field');
elem.attr('class', "required");
}
}

if (shipto_country !="") {
if (shipto_country == '222') {
var elem = jQuery('#shipto_zip_field');
elem.attr('class', "validate-UKzip");
} else {
var elem = jQuery('#shipto_zip_field');
elem.attr('class', "required");
}
}
//quorva UK zip check end
f.task.value = t; //this is a method to set the task of the form on the fTask.
if (document.formvalidator.isValid(f)) {
f.submit();
return true;
} else {
var msg = '<?php echo addslashes (JText::('COM_VIRTUEMART_USER_FORM_MISSING_REQUIRED_JS')); ?>';
alert(msg + ' ');
}
return false;
}

function callValidatorForRegister(f) {

var elem = jQuery('#username_field');
elem.attr('class', "required");

var elem = jQuery('#name_field');
elem.attr('class', "required");

var elem = jQuery('#password_field');
elem.attr('class', "required");

var elem = jQuery('#password2_field');
elem.attr('class', "required");

var elem = jQuery('#userForm');

return myValidator(f, '<?php echo $rtask ?>');

}
</script>




If you have a better suggestions as to how to do this then I am very willing to test. 

At the moment I am happy that this provides enough for a UK trader however, no doubt as soon as this is available, users will scream that they want to be able to disable it!

Possible problems:- 
1. Functionality does not consider that either the Country or Zip might be disabled
2. You cannot turn it off without a template override.

Oh no! I think that this needs a different approach to be put into core!
  :'(

I will let you decide!

Regards
A

Joomla 4.4.5
php 8.1

interfmtas

Do you guys have any update on this, im trying to get it working on with Brazilian zip codes (cep) without success. Any tips?

Maxim Pishnyak

What is the problem with Brazilian zip codes?
You can support Community by voting for Project on the JED
https://extensions.joomla.org/extension/virtuemart/#reviews
Join us at
https://twitter.com/virtuemart