Skip to content Skip to sidebar Skip to footer

Retaining Substring Pattern Using Jquery Or Plain Javascript

A textbox has a property of storing a string which has a type telephone number or zip code. Where and when a user starts typing the keys should fill the textbox in a telephone/zip

Solution 1:

From page. you can use following code . you can test it on jsfiddle

  $(function() {
    $("input[name='phone']").keyup(function() {
        var curchr = this.value.length;
        var curval = $(this).val();
        if (curchr == 3) {
            $(this).val("(" + curval + ")" + "-");
        } else if (curchr == 9) {
            $(this).val(curval + "-");
        }
    });
});​

Post a Comment for "Retaining Substring Pattern Using Jquery Or Plain Javascript"