473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

form validation: postal / zip codes

I used some of the excellent resources from DITHERING.COM for help in my
groveling newbie attempts to cough up working form validation....

I cut and pasted bits of code to check USA ZIP codes and CANADIAN POSTAL
codes, and merged them into one function that I called validCode. The
<form> tag has an onSubmit call to a general form-checker that works fine to
make sure all fields are filled. But within the form is a ZIP/POSTAL CODE
field, where the onBlur calls the function below: "
onBlur="validCode();".... The form-checking works fine, but when the
"onBlur" is engaged, it gives me an "object expected" error where I'm
defining the "var zip".

If any of you seasoned warriors could lend me a neuron or 2, I owe you a
cappuchino or more...

=============================

function validCode(zip) {
var zip = removeSpaces(zip);
if (!(zip.length == 5 || zip.length == 9 || zip.length == 10)) return
false;
if ((zip.length == 5 || zip.length == 9) && !isNumeric(zip)) return
false;
if (zip.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/)
== -1) return false;
return true;
// Check that a Canadian postal code is valid
if (zip.search) {
zip = removeSpaces(zip);
if (zip.length == 6 && zip.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/)
!= -1) return true;
else if (zip.length == 7 &&
zip.search(/^[a-zA-Z]\d[a-zA-Z]-\d[a-zA-Z]\d$/) != -1) return true;
else return false;
}
return true;
}
// ---> END
</script>
=============================
Jul 23 '05 #1
2 8348
Axel Foley wrote:
I used some of the excellent resources from DITHERING.COM for help in my
groveling newbie attempts to cough up working form validation....

I cut and pasted bits of code to check USA ZIP codes and CANADIAN POSTAL
codes, and merged them into one function that I called validCode. The
<form> tag has an onSubmit call to a general form-checker that works fine to
make sure all fields are filled. But within the form is a ZIP/POSTAL CODE
field, where the onBlur calls the function below: "
onBlur="validCode();".... The form-checking works fine, but when the
"onBlur" is engaged, it gives me an "object expected" error where I'm
defining the "var zip".

If any of you seasoned warriors could lend me a neuron or 2, I owe you a
cappuchino or more...

=============================

function validCode(zip) {
var zip = removeSpaces(zip);


You are calling onBlur="validCode();"
but it needs to be:
onBlur="validCode(this.value);"

The function is expecting a parameter, tries to pass a parameter, and
you aren't passing the parameter.

Also, don't use onBlur to check it, use onChange instead. Then, it only
gets checked when its actually been changed. Tabbing through the form
will cause the onBlur to be needlessly fired.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #2
JRS: In article <vgRCc.14444$HS3.10438@edtnps84>, seen in
news:comp.lang.javascript, Axel Foley <de*******@synchkat.com> posted at
Fri, 25 Jun 2004 08:21:47 :
I used some of the excellent resources from DITHERING.COM for help in my ?????????groveling newbie attempts to cough up working form validation....

US codes look like 12345, 123456789, 12345-6789
CA codes look like B2B2B2, B2B 2B2

Consider

function validCode(zip) { var OK // or \s? v
OK = /^(\d{5}|\d{9}|\d{5}-\d{4}|[a-z]\d[a-z]\s*\d[a-z]\d)$/i.test(zip)
return OK }
function validCode(zip) {
var zip = removeSpaces(zip);
...
// Check that a Canadian postal code is valid
if (zip.search) {
zip = removeSpaces(zip);


Did you not already remove them?

IMHO, removing too many spaces is a mistake; for example, Ol 944 5683
has 9 digits, but is much more likely to be an old munged UK phone
number than a new US Zip.

RegExp tested in <URL:http://www.merlyn.demon.co.uk/js-valid.htm#RT>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: webbedfeet | last post by:
Hi I hope someone can help me. I have a client side form validation script which works perfectly in IE but clicking "Submit" in Mozilla does nothing - the form won't submit. Is there something I...
5
by: Kevin Newman | last post by:
Does anyone know of any application (AJAX or other) that will display the appropriate address for for the selected (or detected) country? If not, does anyone know where I can find a list or...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
1
by: Rusty Hill | last post by:
My web app is going to support 26 different cultures and languages. I understand how to handle the basic localization. How do I localize the validation controls and expressions that will be used...
15
by: MKO | last post by:
Hi guys I'm having a little trouble with form validation, I can get the entire form to validate without problems (i.e. when all fields set to required & valid data is entered) but can't get it to...
4
by: racshah | last post by:
Hi I have been asked to validate postal/zip codes against the countries like Netherland, United Kingdom, Spain, Italy, Belgium etc. Can anyone suggest that how can I do it programmatically using...
7
by: creative1 | last post by:
I am trying form valdiation with javascript but it never call the function. can someone please test this code and see why it is like that. It screwed up my mind. here is my code: <!-- Java Script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.