Insert dash after every 2nd character in input using JavaScript - Example looks like - < script > $ ( '.shortCodeText' ). keyup ( function () { var shortCode = $ ( this ). val (). split ( "-" ). join ( "" ); // remove hyphens if ( shortCode . length > 0 ) { shortCode = shortCode . match ( new RegExp ( '.{1,2}' , 'g' )). join ( "-" ); } $ ( this ). val ( shortCode ); }); $ ( '#btnclick' ). on ( 'click' , function (){ console . log ( $ ( '.shortCodeText' ). val ()); }); </ script > And HTML < div > < input type = "text" class = "shortCodeText" max = "6" maxlengthh = "6" /> < a id = "btnclick" > Result </ a > </ div > And the live Result - http://jsfiddle.net/26h...
Angular, React, JavaScript, Java, PHP, SQL, C#, Vue, NodeJs, TypeScript and Interview Questions Answers