[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Magento: Zipcode Validation Fix

Posted on 2010-08-30
7
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

656 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