[Edit] :
keypress
event will be more appropriate.
Try
keypress
event of jquery :
First allow only 7 characters in your text box by using
maxlength
property
<input type="text" id="txtInput" maxlength="7" />
then write
keypress
event in jquery :
$(document).ready(function () {
$('#txtInput').keypress(function (e) {
var inputVal = $(this).val();
if (e.keyCode != 8) {
if (inputVal.length == 4) {
inputVal = inputVal + '-';
$(this).val(inputVal);
}
}
});
});
Good luck.