Jeremy Smith |
04-23-2009 06:07 AM |
Register form post code validation script & possible password script
I've got another problem.
I have been asked to do some form validation on our site but the script I am using just refuses to work.
What I have done is, put the following javascript code into ' /customer/home.tpl'
HTML Code:
{literal}
<SCRIPT LANGUAGE="JavaScript">
function postit() { //check postcode format is valid
test = document.registerform.b_zipcode.value; size = test.length
test = test.toUpperCase(); //Change to uppercase
while (test.slice(0,1) == " ") //Strip leading spaces
{test = test.substr(1,size-1);size = test.length
}
while(test.slice(size-1,size)== " ") //Strip trailing spaces
{test = test.substr(0,size-1);size = test.length
}
document.registerform.b_zipcode.value = test; //write back to form field
if (size < 6 || size > 8){ //Code length rule
alert(test + " is not a valid postcode - wrong length");
document.registerform.b_zipcode.focus();
return false;
}
if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
alert(test + " is not a valid postcode - cannot start with a number");
document.registerform.b_zipcode.focus();
return false;
}
if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
alert(test + " is not a valid postcode - alpha character in wrong position");
document.registerform.b_zipcode.focus();
return false;
}
if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
alert(test + " is not a valid postcode - number in wrong position");
document.registerform.b_zipcode.focus();
return false;
}
if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
alert(test + " is not a valid postcode - number in wrong position");
document.registerform.b_zipcode.focus();
return false;
}
if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
alert(test + " is not a valid postcode - no space or space in wrong position");
document.registerform.b_zipcode.focus();
return false;
}
count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
if (count1 != count2){//only one space rule
alert(test + " is not a valid postcode - only one space allowed");
document.registerform.b_zipcode.focus();
return false;
}
alert("Postcode Format OK");
return true;
}
</script>
{/literal}
I have as you can see I have changed the form object model variables to co align with the form.
I then added this in on the actual register form for customers '/customer/main/register.tpl ':
HTML Code:
{if $js_enabled and $usertype eq "C"}
{include file="buttons/submit.tpl" type="input" style="button" href="javascript: return check_registerform_fields() + postit(); return false;"}
{else}
<input type="submit" value=" {$lng.lbl_submit|strip_tags:false|escape} " />
{/if}
But it keeps coming up with this error in FF v3:
Error: missing } in XML expression
Source File: http://mydomain.com/register.php?
Line: 24, Column: 1
Source Code:
test = test.toUpperCase(); //Change to uppercase
It works in normal html as such, with the following criteria for a successfull postcode validation script: - The total length must be 6,7, or 8 characters, a gap (space character) must be included
- The inward code, the part to the right of the gap, must always be 3 characters
- The first character of the inward code must be numeric
- The second and third characters of the inward code must be alpha
- The outward code, the part to the left of the gap, can be 2,3, or 4 characters
- The first character of the outward code must be alpha
Can someone help please?
Thanks ever so much,
Jeremy.
|