VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Development & Testing => Topic started by: AH on May 24, 2013, 21:57:27 PM

Title: validate UK zip
Post by: AH on May 24, 2013, 21:57:27 PM
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>
Title: Re: validate UK zip
Post by: Milbo on May 24, 2013, 22:02:15 PM
You must add it in our js formvalidator somewhere and in the userfield itself.
Title: Re: validate UK zip
Post by: AH on May 28, 2013, 21:24:25 PM
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.
Title: Re: validate UK zip
Post by: Milbo on June 04, 2013, 16:27:52 PM
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
Title: Re: validate UK zip
Post by: AH on June 10, 2013, 21:37:34 PM
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??
Title: Re: validate UK zip
Post by: Milbo on June 13, 2013, 18:32:40 PM
Things I may do then. If you provide a patch with your knowledge
Title: Re: validate UK zip
Post by: AH on June 13, 2013, 19:28:53 PM
How do you want the code?

Title: Re: validate UK zip
Post by: Milbo on June 14, 2013, 11:49:08 AM
at best as patch to the currenct svn http://dev.virtuemart.net/projects/virtuemart/wiki/Setting_up_a_Development_Environment
Title: Re: validate UK zip
Post by: AH on June 14, 2013, 18:14:53 PM
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!

Title: Re: validate UK zip
Post by: interfmtas on August 01, 2013, 21:24:39 PM
Do you guys have any update on this, im trying to get it working on with Brazilian zip codes (cep) without success. Any tips?
Title: Re: validate UK zip
Post by: Maxim Pishnyak on August 02, 2013, 10:08:14 AM
What is the problem with Brazilian zip codes?