Click to See Complete Forum and Search --> : Adding a dash to a postal code


Leo67
10-09-2003, 02:36 PM
Hi,

Charles had helped me earlier today with form validation for Users who could enter either a postal code or zip code into the same input field. I have looked at the code on the site he had recommended to me devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html) and I am trying to add a - character after the third character in the Postal Code but of course not adding the - after the third character of the Zip Code.
The code that is currently in place is:
String.prototype.isPostalCode = function () {return /^([A-Za-z]\d){3}$/.test(this) || /^\d{5}(-\d{4})?$/.test(this)}

The code I think I should add into the current code has been highlighted in red .... and I keep getting a syntax error for that line.

String.prototype.isPostalCode = function () {return /^([A-Za-z]\d){3}$/([-]){4}$/.test(this) || /^\d{5}(-\d{4})?$/.test(this)}

Charles
10-09-2003, 03:46 PM
This will allow, but not require, that hyphen.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
String.prototype.isPostalCode = function () {return /^[A-Za-z]\d[A-Za-z]-?\d[A-Za-z]\d$/.test(this) || /^\d{5}(-\d{4})?$/.test(this)}
// -->
</script>

<form action="">
<div>
<label>Postal Code<input type="text" onchange="if (!this.value.isPostalCode()) {alert('That would not appear to be a valid postal code.'); this.value=''; this.focus()}"></label>
<button type="submit">Submit</button>
</div>
</form>

Leo67
10-09-2003, 03:53 PM
Thanks Charles ... is there any way to ensure the dash is included in the Postal Code or does the problem reside with the fact the Zip code is also being validated at the same time?

Charles
10-09-2003, 03:59 PM
String.prototype.isPostalCode = function () {return /^[A-Za-z]\d[A-Za-z]-\d[A-Za- z]\d$/.test(this) || /^\d{5}(-\d{4})?$/.test(this)}

The two are validated separately. Or rather, the String is checked against the Zip Code only if it is found to not be a Postal Code.

Leo67
10-09-2003, 04:11 PM
The latest string seems to be giving an error message:
"Invalid range in character set"

The only thing I notice that is different is the red questionmark is missing from the latest string ...

String.prototype.isPostalCode = function () {return /^[A-Za-z]\d[A-Za-z]-?\d[A-Za- z]\d$/.test(this) || /^\d{5}(-\d{4})?$/.test(this)}

:(

Charles
10-09-2003, 04:22 PM
You have a cut and paste error there, an extra space just before the last "z".

That "?" makes whatever is before it optional. In this case that would be the hyphen.

And note that the brackets define a class of charaters and that the hyphen has a special meaning inside those brackets.

Keep reading the documentation on regular expressions. The learning curve is steep but short.

Leo67
10-09-2003, 04:33 PM
I keep going over the information on the site and still have not reached enlightenment. Fixed the spacing issue before the "z" but still am struggling with the 4th character which MUST = the "-" character ..... :confused:

Leo67
10-09-2003, 04:35 PM
... but only for the 4th character of a postal code, not for a Zip code.

Charles
10-09-2003, 07:52 PM
If you are still using the "filter" that I demonstrated on the other thread, you should know that it filters out hyphens. Try this one instead.

this.value.replace(/[^\w\-]/g, '').toUpperCase()

Leo67
10-10-2003, 11:20 AM
thank you Charles :)