I have two different criteria for two seperate form feilds both using regular expressions, however for some reason the second one is the only one which is applied. The code ignors the first rule no matter which way round they go. could someone please rewrite for me?
here is my JS:
Code:
<script language="JavaScript" type="text/javascript">
//<![CDATA[
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}$/;
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
}
return true
}
var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
function checkForm(aForm) {
method 'test()' */
if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
aForm.shipPostalCode.focus()
aForm.shipPostalCode.select()
return false
}
return true
}
//]]>
</script>
You can't have two function's with the same name.
Might also be useful to see where/how you are trying to call them.
Also, your RegExp for a UK Postcode doesn't look right - the first group is one or two letters, followed by one or two digits... Probably worth having a look on the net for a pre-built expression, as some of the rules can be pretty subtle.
And omnicity picked out the one thing that is going to kill the validation from the start - you canNOT have two functions with the same name in the same document (either typed in or refereced by src.) That's the main thing, right there. Conflict. One gets ignored; assuming it doesn't flat out break.
right ok i see so i need to combine the two together into one function? how would I go about doing this? my javascript knowledge is very patchy im afraid!
Also, your RegExp for a UK Postcode doesn't look right - the first group is one or two letters, followed by one or two digits... Probably worth having a look on the net for a pre-built expression, as some of the rules can be pretty subtle.
thanks for the heads up and i am aware that there are alot more rules which can be applied when validating the postcode, however for what i need it for this rule is all i need. i appreciate your help however.
Some of the London postcodes can be as simple as N1 1AA or similar - your RegExp only accepts ones with exactly two initial letters, but if you are sure that doesn't matter, then the simplest re-write gives you:
Code:
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 regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
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
}
if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
aForm.shipPostalCode.focus()
aForm.shipPostalCode.select()
return false
}
return true
}
Brilliant thank you! yes i am aware that some postcodes wont validate as it is but this form is only going to be used by a select set of people in a certain area all of whom have postcodes which will validate. just want to prevent them from making mistakes really!
your code does indeed work! thank you so much!
Originally Posted by omnicity
Some of the London postcodes can be as simple as N1 1AA or similar - your RegExp only accepts ones with exactly two initial letters, but if you are sure that doesn't matter, then the simplest re-write gives you:
Code:
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 regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
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
}
if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
aForm.shipPostalCode.focus()
aForm.shipPostalCode.select()
return false
}
return true
}
Bookmarks