In: Computer Science
JAVASCRIPT HTML
I'm looking to make a function that will take a postal code in a text box. The function jobs is to basically make sure the first letter of the postal code starts with these letters. ('A') ('N") ('W') ('F'). If the first letter of the postal code does not match up then an error to the user is sent.
HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<!-- title for web page -->
<title>Postal Code Check</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- label for postal code -->
<label for="postalCode">Postal Code</label>
<!-- textbox for postal code -->
<input type="text" id="txtPostalCode"/>
<!-- button to check postal code -->
<input type="button" value="Check Postal Code" onclick="jobs()"/>
<br>
<!-- div to display postal code -->
<div id="myDiv"></div>
<!-- script is used for javascript -->
<script>
//function jobs
function jobs()
{
//This line will take the postal code entered by user
var postalCode=document.getElementById("txtPostalCode").value;
//This line will take first character of postal code
var firstChar=postalCode.charAt(0);
//checking first character
if(firstChar.toUpperCase()=='A'||firstChar.toUpperCase()=='N'||firstChar.toUpperCase()=='W'||firstChar.toUpperCase()=='F')
{
//when first character is either A,N,W,F then postal code is valie
document.getElementById("myDiv").innerHTML="Postal code "+postalCode+" is valid";
}
else{
//when first character is not any of A,N,W,F then postal code is valie
document.getElementById("myDiv").innerHTML="Postal code "+postalCode+" is invalid";
}
}
</script>
</body>
</html>
=============================================
Screen when postal code is valid :
Screen when postal code is invalid :