Solved

Magento: Zipcode Validation Fix

Posted on 2010-08-30
7
259 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maps stopped work unsure why 7 34
Angular - code works but need to make a change 14 30
Using jQuery to set a DIV attribute on page load 9 29
how can i test my code? 1 33
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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

786 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