代码之家  ›  专栏  ›  技术社区  ›  Peter

电话屏蔽功能在窗体字段中插入额外的“x”

  •  0
  • Peter  · 技术社区  · 7 年前

    我的电话屏蔽脚本是插入正确的破折号,但与扩展它是添加额外的数字 "x's" 在第12个字符之后。我正试图格式化面具,使它看起来像 000-0000x0000000 000-000-0000xxxx0000 . 你知道我做错了什么吗?

    $(document).ready(function() {	
    	var phoneNumber = $('#phone_number');
    	
    	// Adds a phone number mask
      	phoneNumber.on('input paste', function(e) {
    		var phoneNumStr = e.target.value.split("-").join("");
    
    		// Create a new string with the hyphen
    		pro = phoneNumStr.split('').map(function(str, index) {
    		
    			// Inserts a hyphen after the third and sixth characters
    			if (index == 3 || index == 6) {
    				return "-" + str;
    			} else if (index == 10) {
    				return "x" + str;
    			} else {
    				return str;
    			}
    		}).join('');
    		
    		// Returns the new string
    		$(this).val(pro);
    	});
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label for="phone_number">Phone Number*</label> 
    <input type="text" id="phone_number" name="phone_number" maxlength="20">
    2 回复  |  直到 7 年前
        1
  •  1
  •   Taplar    7 年前

    将拆分“-”更改为使用正则表达式在破折号上拆分,并使用x从计算的字符串中删除x。

    $(document).ready(function() {	
    	var phoneNumber = $('#phone_number');
    	
    	// Adds a phone number mask
      	phoneNumber.on('input paste', function(e) {
                                             //remove dash and x
    		var phoneNumStr = e.target.value.split(/[x-]/).join("");
    
    		// Create a new string with the hyphen
    		pro = phoneNumStr.split('').map(function(str, index) {
    		
    			// Inserts a hyphen after the third and sixth characters
    			if (index == 3 || index == 6) {
    				return "-" + str;
    			} else if (index == 10) {
    				return "x" + str;
    			} else {
    				return str;
    			}
    		}).join('');
    		
    		// Returns the new string
    		$(this).val(pro);
    	});
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label for="phone_number">Phone Number*</label> 
    			<input type="text" id="phone_number" name="phone_number" maxlength="20">
        2
  •  0
  •   Arun Kumar    7 年前

    尝试以下代码

    $(document).ready(function() {  
    var phoneNumber = $('#phone_number');
    
    // Adds a phone number mask
    phoneNumber.on('input paste', function(e) {
        var phoneNumStr = e.target.value.split("-").join("");
    
        // Create a new string with the hyphen
        pro = phoneNumStr.split('').map(function(str, index) {
    
            // Inserts a hyphen after the third and sixth characters
            if (index == 3 || index == 6) {
                return "-" + str;
            } else if (index == 10) {
                if(str.indexOf("x")==-1)
                  return "x" + str;
                else {
                  return str;
                }
            } else {
                return str;
            }
        }).join('');
    
        // Returns the new string
        $(this).val(pro);
    });