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
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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.