Iv written a regular expression to single out specific postcodes, however it currently returns all postcodes submitted. Could someone take a look at my regular expression and help me correct it?
sorry i need to include all possibilities between MK1 and MK15. Sorry this may not have been clear the first time around. If I use your code but include the additional possibilities from my expression will this work? is there a short hand way of including the numbers between 1 and 15?
iv tried your code using a postcode MK1 however it still displays the alert no matter what postcode is added. here is my full script:
Code:
var regExpressMilton = /^MK(1|15|17|19|77)$/;
function checkForm(aForm) {
if (!regExpressMilton.test(aForm.shipPostalCode.value)) {
window.alert("congratulations! You have been awarded a special gift with your order!")
return true
}
return true
}
ok iv tried this code and now it doesnt display my alert in any situation?
here is my new code?
Code:
var regExpressMilton = /^MK(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)\s$/;
function checkForm(aForm) {
if (regExpressMilton.test(aForm.shipPostalCode.value)) {
window.alert("congratulations! You have been awarded a special gift with your order!")
return true
}
return true
}
I see from your test that it seems to be working, so what is wrong with my code? really stuck on this! :s
var regExpressMilton = new RegExp("mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]");
function checkForm(aForm) {
var a = regExpressMilton.test(aForm.shipPostalCode.value);
if (a) {
window.alert("congratulations! You have been awarded a special gift with your order!")
return true
}
return true
}
Iv tried this and it doesnt seem to be giving me an alert message? perhaps if I show you my full code you will better udnerstand why I used the method I have.
Code:
var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}\s([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
var regExpressFidelity = /^[a-zA-Z]{1}[a-zA-Z0-9]{4}\s*[a-zA-Z0-9]{5}\s*[a-zA-Z0-9]{3}\d{1}(@|!|\&){1}$/;
var regExpressMilton = /^MK(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]$/;
function checkForm(aForm) {
if (!regExpressFidelity.test(aForm.fidelityCardNumber.value)){
window.alert("Invalid Card Number. Please check your number and try again.")
aForm.fidelityCardNumber.focus()
aForm.fidelityCardNumber.select()
return false
}
// check for a valid postcode address format using the RegExp object method 'test()'
if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
//gives focus to field and selects incorrect postcode format text
aForm.shipPostalCode.focus()
aForm.shipPostalCode.select()
return false
}
if (regExpressMilton.test(aForm.shipPostalCode.value)) {
window.alert("congratulations! You have been awarded a special gift with your order!")
return true
}
return true
}
ah iv done it! it was the $ i had on the end of the string which was ending the expression! removed it and it now works. here is the finished full code for anyone interested!
Code:
<script type="text/javascript">
//<![CDATA[
// create a regular expression to check UK postcode format
var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}\s([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
var regExpressFidelity = /^[a-zA-Z]{1}[a-zA-Z0-9]{4}\s*[a-zA-Z0-9]{5}\s*[a-zA-Z0-9]{3}\d{1}(@|!|\&){1}$/;
// var regExpressMilton = new RegExp("mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]");
var regExpressMilton = /^MK(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]/;
function checkForm(aForm) {
if (!regExpressFidelity.test(aForm.fidelityCardNumber.value)){
window.alert("Invalid Card Number. Please check your number and try again.")
aForm.fidelityCardNumber.focus()
aForm.fidelityCardNumber.select()
return false
}
// check for a valid postcode address format using the RegExp object method 'test()'
if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
//gives focus to field and selects incorrect postcode format text
aForm.shipPostalCode.focus()
aForm.shipPostalCode.select()
return false
}
if (regExpressMilton.test(aForm.shipPostalCode.value)) {
window.alert("Congratulations! You have been awarded a special gift with your order!")
return true
}
return true
}
//]]>
</script>
<html><head><script language="JavaScript" type="text/javascript">
var regExpressMilton = new RegExp("(mk|MK)(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]");
function checkForm() {
var postalcode = document.getElementById("shipPostalCode").value;
var a = regExpressMilton.test(postalcode);
if (a) {
window.alert("congratulations! You have been awarded a special gift with your order!");
}
return true
}
</script></head><body><form id="form1"><input id="shipPostalCode" /><br /><a onclick="checkForm();">Check</a></form></body></html>
Bookmarks