CancelImage Upload

Validating a Latvian Postcode using JavaScript and Regular Expressions

In Latvia Postcodes consist of two letters followed by a hyphen and four digits:

LV-3014

Here then is a regular expression that can be used to validate the above postcode format. To limit the first two letters to capitols simply remove the lowercase a-z:

^([a-zA-Z]){2}[-]([0-9]){4}$

For more extensive introduction to the above regular expression here is a JavaScript Latvian Postcode validation code:

<html>
<head>
<title>Latvian Postcode Validation with JavaScript</title>

<script language="JavaScript">

function postcode_validate(postcode)
{

var regPostcode = /^([a-zA-Z]){2}[-]([0-9]){4}$/;

obj = document.getElementById("status");

if(regPostcode.test(postcode) == false)
{

obj.innerHTML = "Postcode is not yet valid.";

}
else
{

obj.innerHTML = "Your postcode is valid!";

}

}

</script>

<style type="text/css">
<!--
body {
background:#CCCCFF;
}

div {
width: 100%;
text-align: center;
margin-top:150px;
}

span {
color: #000099;
font: 8pt verdana;
font-weight:bold;
text-decoration:none;
}

input {
color: #000000;
background: #F8F8F8;
border: 1px solid #353535;
width:250px;
font: 8pt verdana;
font-weight:normal;
text-decoration:none;
margin-top:5px;
}
-->
</style>
</head>
<body>

<div>
<span id="status">Please enter a valid postcode.</span><br>
<input type="text" name="postcode" onkeyup="postcode_validate(this.value);">
</div>

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com