Solved

Magento: Zipcode Validation Fix

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
[form-control] Retain values after a POST action 21 68
hide and show spans from dropdown selection 3 34
Ajax success not firing alert 6 36
Javascript Error 10 21
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

738 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