I am creating a site that requires a very small postcode checker. I have approx 8 postcode prefix's, HX, HD, BD, LS etc in an array. I also have a simple input field and submit btn. When the user types in a postcode for example HX5 9DU I want Jquery to check the array, if there is match for the first 2/3 letters I want a div to fade in displaying a message.
How would I do this? Many thanks in advance.
Here's one way to do it:
http://jsfiddle.net/uNKhs/3/
HTML:
jQuery:
Some related jQuery docs:
.hide()
http://api.jquery.com/hide/$.inArray()
http://api.jquery.com/jQuery.inArray/.fadeIn()
http://api.jquery.com/fadein/.keyup()
http://api.jquery.com/keyup/.val()
http://api.jquery.com/val/EDIT:
When running jQuery code, it is usually best to have your code run after the document has loaded. You can do this a couple different ways.
or
The two will accomplish the same thing.
I updated my answer to include the second version.
BONUS:
This version will update the input with the upper case version if the user types lower case characters for the first two characters.
EDIT: Also, it shows a fail message if a match in the array is not found.
http://jsfiddle.net/uNKhs/8/
I guess it would be best if you involve some serverside actors to this episode.
Doing something like:
You can store all your codes in a db, then just query the db with the param you get in the json call and craft the message on your server and send it to the UI.