• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Magento: Zipcode Validation Fix

I have a zip code validation code for my Magento store, it verifies certain countries zip codes. For instance Germany needs to have at least 5 digits or else it comes up with an error.  However when you go beyond 5 digits there is no error.  I need an error to report if its beyond 5 digits. Also attached to the code is for Romania that requires 7 digits.
Validation.addAllThese(

   
        

                var bill_country_id = 'DE';

                var bill_country = $('billing:country_id');

                var element = $('billing:postcode');

                if (element && ('' != element.value) && (bill_country_id == bill_country.value)) {

                     if (!element.value.match(/[0-9]{5}/)) {

                        return false;

                    }

                }

                var bill_country_id = 'RO';

                var bill_country = $('billing:country_id');

                var element = $('billing:postcode');

                if (element && ('' != element.value) && (bill_country_id == bill_country.value)) {

                     if (!element.value.match(/[0-9]{7}/)) {

                        return false;

                    }

                }

                     return true;

           }]

    ]

    );

Open in new window

0
sheeshkabob
Asked:
sheeshkabob
  • 4
1 Solution
 
leakim971PluritechnicianCommented:
replace match by test

match : http://www.w3schools.com/jsref/jsref_match.asp
test : http://www.w3schools.com/js/js_obj_regexp.asp
if (!element.value.test(/[0-9]{5}/)) {

Open in new window

0
 
sheeshkabobAuthor Commented:
i replaced
if (!element.value.test(/[0-9]{5}/)) {


from the orginal

if (!element.value.match(/[0-9]{5}/)) {


but does not work, do I need to do anything with the files that you linked over? include them?

0
 
leakim971PluritechnicianCommented:
Ok, that should work with this one :




if(/^\d{5}$/.test(element.value)) {

Open in new window

0
 
leakim971PluritechnicianCommented:
the right one (to return false if the not 5 only) :


if(!/^\d{5}$/.test(element.value)) {

Open in new window

0
 
leakim971PluritechnicianCommented:
labo :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	function $(o) {
		return document.getElementById(o);
	}
	function validateBill() {
//		var bill_country_id = 'DE';
//		var bill_country = $('billing:country_id');
		var element = $('billing:postcode');
//		if (element && ('' != element.value) && (bill_country_id == bill_country.value)) {
			if (!/^\d{5}$/.test(element.value)) {
				return false;
			}
//		}
		return true;
	}
</script>
</head>
<body onload="if(validateBill()) alert('valid');else alert('not valid!')">
<input type="text" value="12345" id="billing:postcode" onblur="if(validateBill()) alert('valid');else alert('not valid!')" />
</body>
</html>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now