Jump to content

Adding a space to postcode


yogibear

Recommended Posts

Hi

 

At the moment I have a textbox for a post code text box that when the user clicks submit, if the input doesn’t contain a space and alert is displayed saying post code must contain a space. How can I change it so that it adds a space after the 2nd character? I know this isn’t perfect but it will prevent errors on the next page (not controlled by me) saying the post code must contain a space (the postcode is not displayed to user).

 

Thanks in advanced

Best wishes

Yogi

Link to comment
https://forums.phpfreaks.com/topic/125942-adding-a-space-to-postcode/
Share on other sites

What country postcode are you working on? If it's UK, then it might be third character (can't rem if theres possibility of 4). Any which way though...

if, if, if
document.write(pc.substring(0, 3) + " " + pc.substring(4, pc.length));

this might need a slight tweet since it's not tested...

I would suggest, that if their are two parts to the post code that you supply two text fields.

But at the end of the form have a hidden form input which will be for the normal postcode. Then you can do something like this:

 

<script language="javascript">
function set_post_code(){
document.getElementById("postcode").value = document.getElementById("postcode_one").value + " " + document.getElementById("postcode_two").value;
}
</script>

 

Then in your submit button:

 

<input type="submit" name="submit" id="submit" value="Send" onclick="javascript: set_post_code()" />

 

That way you can also sat max lengths on the post code boxes and check with javascript too see that they are the appropriate length.

 

Just a suggestion, there are many ways you could go about this.

What country postcode are you working on? If it's UK, then it might be third character (can't rem if theres possibility of 4).

 

I'm fairly sure the first part of a UK postcode can be 2, 3 or 4 characters. The last part, however, is always 3.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.