php - how to make a patteren like 12345-1234567-8 using javascript or jquery? -
i need midify code mean after 5 character hyphen "-" auto inserted , after 7 character hyphen "-" inserted. in code after 5 character hyphen inserted.
<html> <head> <script> $('.creditcardtext').keyup(function() { var foo = $(this).val().split("-").join(""); // remove hyphens if (foo.length > 0) { foo = foo.match(new regexp('.{1,5}', 'g')).join("-"); } $(this).val(foo); }); </script> </heead> <body> <input type="text" class="creditcardtext" /> </body> </html>
you can match whole string of numbers, , split them chunks this:
var num = "1234512345671"; alert("parsed: " + num.replace(/(\d{5})(\d{7})(\d)/, "$1-$2-$3"));
Comments
Post a Comment