我实现了一个自动添加空格的函数,如下所示:
或者
这是脚本代码:
+33 6 XX XX XX XX
或者
06 XX XX XX XX
这是脚本代码:
$('#num').keypress(function(){
$this = $(this);
//remove whitespaces to calculate the length of the string
$val = $this.val().replace(/\s+/g, '');
//if it's the case '+33 6 XX XX XX XX
if ($val.toLowerCase().indexOf("+") >= 0 | $val == "")
{
if($val.length == 3){
$this.val($this.val() + " ");
}else if($val.length == 4){
$this.val($this.val() + " ");
}else if ($val.length >= 5 && ($val.length)%2 == 0){
$this.val($this.val() + " ");
}
}else{
if (($val.length)%2 == 0){
$this.val($this.val() + " ");
}
}
});
在chrome上它可以完美工作,但在firefox上我无法使用退格键删除输入的内容.. 有什么想法吗?
这里是一个jsfiddle来说明: