Solved

Magento: Zipcode Validation Fix

Posted on 2010-08-30
7
254 Views
Last Modified: 2013-11-19
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
Comment
Question by:sheeshkabob
  • 4
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33563431
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
 

Author Comment

by:sheeshkabob
ID: 33564031
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33567594
Ok, that should work with this one :




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

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33567613
the right one (to return false if the not 5 only) :


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

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33567642
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now