/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Regular Expression

Hi Gurus!

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?

[CODE]/^[M]{1}[K]{1}([1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|17|19|77]){1,2}$/;[/CODE]

I am trying to single out only postcodes which start with MK1 – MK15, MK17, MK19 and MK77

Any ideas?

Thanks for any help!

Mike

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@Rufe0Apr 13.2011 — mk(15|17|19|77|1)s
Copy linkTweet thisAlerts:
@mike-web-devauthorApr 13.2011 — 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?

Thanks.
Copy linkTweet thisAlerts:
@mike-web-devauthorApr 13.2011 — 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
}[/CODE]
Copy linkTweet thisAlerts:
@Rufe0Apr 13.2011 — mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)s
Copy linkTweet thisAlerts:
@Rufe0Apr 13.2011 — [code=html]<html>
<body>

<script type="text/javascript">
var regexp1=new RegExp("mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)s");

document.write(regexp1.test("mk1 gh45"));
document.write("<br />");
document.write(regexp1.test("aa1 aa43"));
</script>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@mike-web-devauthorApr 13.2011 — 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
}[/CODE]


I see from your test that it seems to be working, so what is wrong with my code? really stuck on this! :s

Thanks!
Copy linkTweet thisAlerts:
@Rufe0Apr 13.2011 — [code=html]<html>
<body>

<script type="text/javascript">
var regexp1=new RegExp("mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]");

document.write(regexp1.test("mk1 gh45"));
document.write("<br />");
document.write(regexp1.test("aa1 aa43"));
</script>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@Rufe0Apr 13.2011 — try this
[code=html]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
}[/code]
Copy linkTweet thisAlerts:
@mike-web-devauthorApr 13.2011 — 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
}[/CODE]


thanks,

Mike
Copy linkTweet thisAlerts:
@mike-web-devauthorApr 13.2011 — 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>[/CODE]
Copy linkTweet thisAlerts:
@Rufe0Apr 13.2011 — [code=html]<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> [/code]
Copy linkTweet thisAlerts:
@Rufe0Apr 13.2011 — ah cool good good
Copy linkTweet thisAlerts:
@mike-web-devauthorApr 13.2011 — yup got it thank you! all working now! cheers for the help!! i have implimented the (mk|MK) as you suggested also!

Thank you once again!! ?
Copy linkTweet thisAlerts:
@WolfShadeApr 13.2011 — If you do this, you can get rid of the "(mk|MK)" and append the mask with /i (case insensitive) which will test for "mk", "MK", "mK", and "Mk".
<i>
</i>var regExpressMilton = /mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)])/i;

http://www.w3schools.com/jsref/jsref_regexp_test.asp
Copy linkTweet thisAlerts:
@mike-web-devauthorApr 13.2011 — If you do this, you can get rid of the "(mk|MK)" and append the mask with /i (case insensitive) which will test for "mk", "MK", "mK", and "Mk".
<i>
</i>var regExpressMilton = /mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)])/i;

http://www.w3schools.com/jsref/jsref_regexp_test.asp[/QUOTE]


oh woah really? thats brilliant thank you i was unaware of that! i will impliment that across all of my regular expressions! thanks alot!
Copy linkTweet thisAlerts:
@Rufe0Apr 14.2011 — Well yeah you should probably do
[code=html]string.toUpperCase();[/code]

to convert the postcode to capitals anyway
×

Success!

Help @mike-web-dev spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...