• 0

HELP! JavaScript Validating a postalcode :)


Question

Hi,

I started a JavaScript course about one month ago. I really suck, lol!!!!

I?m supposed to write a script to validate Canadian postal code. I wrote the code on Notepad++ every time I try to validate the code with either IE or Firefox neither of them respond. I really don?t know if my code work or not. I?m wondering if one of you can take a look at it and tell me what is wrong and give me feedbacks, pointers, or explain me how I should write it.

My teacher wants me to put something that will change at the end the lowercase to uppercase and also to include a joint (something) to remove the space between the letters.

F.Y.I: I?m not expecting people to do my homework. BUT I truly need help, so any contribution is welcome.

Here goes nothing?lol

html>

<body>

<script language="JavaScript">

function validCodePostale(CodePostale)

{

len=CodePostale.length

nombres="0123456789"

lettres="ABCDEFGHJKLMNPQRSTUVWXYZ"

{if (lettres.indexOf(zip.charAt(0))<0)

{alert("Erreure, mettre une lettre")

break }

if(?nombres?.indexOf(entry.charAt(1))<0)

{alert("Erreure, mettre un nombre")

break }

if(?lettres?.indexOf(entry.charAt(2))<0)

{alert("Erreure, mettre une lettre")

break }

if(?nombres?.indexOf(entry.charAt(3))<0)

{alert("Erreure, mettre un nombre")

break }

if(?lettres?.indexOf(entry.charAt(4))<0)

{alert("Erreure, mettre une lettre")

break }

if(?nombres?.indexOf(entry.charAt(5))<0)

{alert("Erreure, mettre un nombre")

break }

else

{

document.write("<b>Code Postale Valide</b>");

}

</script>

</body>

</html>

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0
&lt;script type="text/javascript"&gt;
function isValidZip(zip)
{
	if(zip.length != 3)
	{
		alert("zip too short");
	}
	zip = zip.toUpperCase();
	zip = zip.replace(' ','');
	var num = "1234567890";
	var let = "abcdefghijklmnopqrstuvwxyz".toUpperCase();
	if(let.indexOf(zip.charAt(0))==-1)
	{
		alert('first letter not valid');
		return false;
	}
	if(num.indexOf(zip.charAt(1))==-1)
	{
		alert('second number not valid');
		return false;
	}
	if(let.indexOf(zip.charAt(2))==-1)
	{
		alert('third letter not valid');
		return false;
	}
	alert('zip is valid');
}
&lt;/script&gt;

&lt;input type="button" onclick="isValidZip('a2a')"/&gt;

Link to comment
Share on other sites

  • 0

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"	"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
	&lt;head&gt;
		&lt;title&gt;Testing Validation&lt;/title&gt;
		&lt;script type="text/javascript"&gt;
		function validate(e) {
		 	field  = document.getElementById("postal-code")
			code = /([abceghjklmnprstvxy]\d[abceghjklmnprstvwxyz])[\s\-]?(\d[abceghjklmnprstvwxyz]\d)/i.exec(field.value)
			if (code == null) alert("Invalid postal code")
			else field.value = (code[1]+code[2]).toUpperCase()
		}
		&lt;/script&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;form action="#" method="get"&gt;
			&lt;input name="postal-code" id="postal-code" maxlength="7" size="7"&gt;
			&lt;input type="button" id="validate" value="Validate" name="validate"&gt;
		&lt;/form&gt;
		&lt;script type="text/javascript"&gt;document.getElementById("validate").onclick = validate&lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;

If I understand the problem the requirements are:

  • Match a canadian postal code entered in the A0A 0A0, A0A-0A0, or A0A-0A0 format (case insensitive)
  • remove any space or hyphen used to separate the two haves so you are left with a 6-character string in A0A0A0 format
  • Convert the postal code to upper case- updating the field
  • Display an alert if the postal code is not a valid format

The above should do all of that while taking into account the restrictions on letters occurring in certain positions.

I have only checked in safari but it doesn't use any features that don't exist in Firefox, Opera, or Internet Explorer so it should run as advertised. If there is an issue just give a shout and I'll fix it.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.