Skip to content
Advertisement

JS – Remove all spaces and insert space three characters from end

I’m trying to format postcode entries in a form correctly so that they have a space in the correct location, which is always three characters from the end. E.g.:

AA1 1AA

A1A 1AA

AA11 1AA

A1 1AA

JavaScript

However, if I type a series of characters into the relevant field it is inserting spaces every other character, in addition to one three characters from the end, e.g.:

1 1 1 1 1 1 111

Where am I going wrong?

Advertisement

Answer

It is becous of the event onKeyUp, you place an extra letter and it keeps adding spaces after every event without removing them.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement