开发者

Jquery US number format facing the problem

Hi In a text box when some one enters a phone number it automatically converts it in to (xxx)-xxx-xxxx this formate, the problem I'm facing is after I enter the phone number and press backspace it is not deleting after (xxx)-xxx , I'm not able to find the problem in the code plz help me.

jquery code

$(document).ready(function()
{
 $('#telephone').keypress(function(e)
 {
  var key = e.charCode || e.keyCode || 0;
  var keychar = String.fromCharCode(key);
  if ( ( ( key == 8 || key == 9 || key == 46 || key == 35 || key == 36 || (key >= 37 && key           <= 40) ) && e.charCode==0 ) || (key >= 48 && key <= 57) && ($('#telephone').val().length < 14) )
  {
   var tlength = $('#telephone').val().length;
   var tvalue = $('#telephone').val();

   if(tlength == 3)
   {
    var tvalue = '(' + $('#telephone').val() + ')'
    $('#telephone').val(tvalue);
   }

   var tlength = $('#telephone').val().length;
   var tvalue = $('#telephone').val();
   if(tlength == 5)
   {
    var tvalue = $('#telephone').val() + '-';
    $('#telephone').val(tvalue);
   }

   var tlength = $('#telephone').val().length;
   var tvalue = $('#telephone').val();
   if( tlength == 9 )
   {
 开发者_C百科   var tvalue = $('#telephone').val() + '-';
    $('#telephone').val(tvalue);
   }
   return;
  } 
  else
  {
   e.preventDefault();
  }
});
});


I think the issue in this line....

if ( ( ( key == 8 || key == 9 || key == 46 || key == 35 || key == 36 || (key >= 37 && key           <= 40) ) && e.charCode==0 ) || (key >= 48 && key <= 57) && ($('#telephone').val().length < 14) )

Try changing the line and replacing it with this:

if ( ( ( key == 8 || key == 9 || key == 46 || key == 35 || key == 36 || (key >= 37 && key           <= 40) ) && key == 0 ) || (key >= 48 && key <= 57) && ($('#telephone').val().length < 14) )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜