Regex 钛合金和正则表达式:无法使用键盘删除文本字段中的字符
此功能:Regex 钛合金和正则表达式:无法使用键盘删除文本字段中的字符,regex,titanium,textfield,appcelerator,Regex,Titanium,Textfield,Appcelerator,此功能: function maskPhoneNumber( phoneNumber ){ //assure that it is a string var stringyPhone = String( phoneNumber ); //refresh the number by stripping it of odd characters stringyPhone = stringyPhone.replace( /[^0-9]/g, '' ); //return only the firs
function maskPhoneNumber( phoneNumber ){
//assure that it is a string
var stringyPhone = String( phoneNumber );
//refresh the number by stripping it of odd characters
stringyPhone = stringyPhone.replace( /[^0-9]/g, '' );
//return only the first 11 digits (if it starts with a 1)
if( stringyPhone.first(1) == 1 ){
stringyPhone = stringyPhone.first( 11 );
}else{
stringyPhone = stringyPhone.first( 10 );
}
stringyPhone = stringyPhone.replace( /^([1]?)(\d{1,3})/, "$1 ($2) " );
stringyPhone = stringyPhone.replace( /(\d{3})(\d{1,4})$/, "$1-$2" );
return stringyPhone;
}
此处称为:
//on change of the phone number
//mask it and set the value of the form to the masked value
$.loginPhone.addEventListener( 'change', function( event ){
$.loginPhone.setValue( maskPhoneNumber( $.loginPhone.getValue() ) );
} );
它在向上的过程中屏蔽得很好(输入产生“1(555)555-5555)”,但是有一个奇怪的错误,它在向下的过程中停止删除(文本字段的退格挂在“1(555)”)
这是我的正则表达式错误,还是我遗漏了什么?任何一个更聪明的人都有一个想法?你的问题是
。替换(/^([1]?)(\d{1,3}/,“$1($2)”)
。每次您退格到1(555)
,它都会替换您刚才删除的空间:1(555)
。