I have a form with the ability to add a zip code (zip code, for American readers), and I want to split the entry into the following format: XXX XXXX.
I use the following JS to split it into three (XXX XXX XXX), but I want it to split only the first three, and then four or more after it.
Used JavaScript:
var ecode = document.getElementById("postcode");
ecode.oninput = function() {
var format = ecode.value.split(" ").join("");
if (format.length > 0) {
format = format.match(new RegExp('.{1,3}','g')).join(" ");
}
this.value = format;
};
I am not familiar with JavaScript and RegEx. Any help is appreciated.
source
share