Jquery美国数字格式面临的问题
您好,在文本框中,当有人输入电话号码时,它会自动将其转换为 (xxx)-xxx-xxxx 这个格式,我面临的问题是在我输入电话号码并按退格键后,它不会删除 ( xxx)-xxx ,我无法在代码中找到问题,请帮助我。
jquery代码
$(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 )
{
var tvalue = $('#telephone').val() + '-';
$('#telephone').val(tvalue);
}
return;
}
else
{
e.preventDefault();
}
});
});
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 )
{
var tvalue = $('#telephone').val() + '-';
$('#telephone').val(tvalue);
}
return;
}
else
{
e.preventDefault();
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在这一行......
尝试更改该行并将其替换为:
I think the issue in this line....
Try changing the line and replacing it with this: