1. Requirements: write a regular
2 that can automatically add a space for every 4 digits written. Problem: the regular (or incorrect) is written out, but the spaces cannot be deleted!
3. Picture
4. Code
innput listening:
`$("ul.information-list").on("input", ".information-value", function(){
var _this = $(this);
var clean = _this.parent().next(".clean");
var value = _this.val().trim();
var length = pubblic.cleanSpace(value).length;
// 4
if (_this.hasClass("card-input")) {
_this.val(pubblic.addSpace(value));
}
//
if(length === 0){
clean.addClass("hide");
} else {
clean.removeClass("hide");
}`
Public method:
// 4
pubblic.addSpace = function (num) {
num = num.replace(/\s/g, "").replace(/(.{4})/g,"$1 ");
return num;
};
//
pubblic.cleanSpace = function (num) {
num = num.replace(/\s+/g, "");
return num;
};
5. This is the relevant part of the code, ask for help!