0

Add hyphen/dash line into html input value automatically. Hope you understand my question. Thanks for the person who can help me

I have this code <html> <head> <title>Add Hyphen after every 3rd character using jQuery</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script> </head> <body> <p>Type some values (numbers or text) in the text box. The script will automatically add a "hyphen" (-) after every 3rd character.</p> <p>The onkeyup event calls a JavaScript fuction after you release the key in text box.</p> <div> <input type="text" id="txt" placeholder="Type some values here" /> </div> </body> <script> $(document).ready(function () { $('#txt').keyup(function (event{ addHyphen (this); }); }); function addHyphen (element) { let val = $(element).val().split('-').join(''); // Remove dash (-) if mistakenly entered. let finalVal = val.match(/.{1,3}/g).join('-'); // Add (-) after 3rd every char. $(element).val(finalVal); // Update the input box. } </script> </html> It adds dash line every 3 character like this 123-456-789. But I want to a dash line like this 123-45-678910.

27th Apr 2019, 11:23 AM
Monkey D Luffy
1 Réponse
0
I sugest you to use data-mask or pattern in your input field. There is alot of example just browsing it. Hopefully thats give you an idea.
27th Apr 2019, 2:19 PM
Andri Hry
Andri Hry - avatar