【问题标题】:Contact form Phone Number format Javascript联系表格电话号码格式Javascript
【发布时间】:2019-11-02 11:12:43
【问题描述】:

这段代码让我可以在我的联系表单中写入一个电话号码,格式为 (XXX) XXX-XXXX。 (工作示例位于https://www.fxmerkezi.com/ucretsiz-danismanlik/

但我需要这样做,例如 0XXXXXXXXXX 第一个字符必须为 0,并且不应允许任何字母或任何其他字符。

这是我头部标签中的代码;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://unpkg.com/jquery-input-mask-phone-number@1.0.0/dist/jquery-input-mask-phone-number.js"></script>

    <script>

        $(document).ready(function () {
            $('#yourphone').usPhoneFormat({
                format: '(xxx) xxx-xxxx',
            });

            $('#yourphone2').usPhoneFormat();
        });

    </script>

这是 jquery-input-mask-phone-number.js 文件:

(function ($) {
$.fn.usPhoneFormat = function (options) {
    var params = $.extend({
        format: 'xxx-xxx-xxxx',
        international: false,

    }, options);

    if (params.format === 'xxx-xxx-xxxx') {
        $(this).bind('paste', function (e) {
            e.preventDefault();
            var inputValue = e.originalEvent.clipboardData.getData('Text');
            if (!$.isNumeric(inputValue)) {
                return false;
            } else {
                inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"));
                $(this).val(inputValue);
                $(this).val('');
                inputValue = inputValue.substring(0, 12);
                $(this).val(inputValue);
            }
        });
        $(this).on('keypress', function (e) {
            if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
            var curchr = this.value.length;
            var curval = $(this).val();
            if (curchr == 3) {
                $(this).val(curval + "-");
            } else if (curchr == 7) {
                $(this).val(curval + "-");
            }
            $(this).attr('maxlength', '12');
        });

    } else if (params.format === '(xxx) xxx-xxxx') {
        $(this).on('keypress', function (e) {
            if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
            var curchr = this.value.length;
            var curval = $(this).val();
            if (curchr == 3) {
                $(this).val('(' + curval + ')' + " ");
            } else if (curchr == 9) {
                $(this).val(curval + "-");
            }
            $(this).attr('maxlength', '14');
        });
        $(this).bind('paste', function (e) {
            e.preventDefault();
            var inputValue = e.originalEvent.clipboardData.getData('Text');
            if (!$.isNumeric(inputValue)) {
                return false;
            } else {
                inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"));
                $(this).val(inputValue);
                $(this).val('');
                inputValue = inputValue.substring(0, 14);
                $(this).val(inputValue);
            }
        });

    }
}

}(jQuery));

【问题讨论】:

  • 那个插件只支持两种手机格式。我建议你完全放弃它,从中学习并编写自己的版本。此正则表达式行是关键:inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"));

标签: javascript


【解决方案1】:
  1. 只需简单地定义您的文本框如下,它将只允许以 0 开头的 11 位数字。

$( document ).ready(function() {

	$( "#number" ).keypress(function(e) {
  
  	if(this.value.length == 0 && e.which != 48){
    	return false
    }
    if(e.which < 48 || e.which > 57 ||  this.value.length > 10){
    	return false
    }
  	
	});	

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="">
<input type="text" id="number" name="country_code" pattern="[0]{1}[0-9]{10}">
    <input type="submit" value="ok">
</form>

【讨论】:

  • 您好,感谢您的帮助,但不限制最大数量
  • 那么您需要在此添加按键事件并检查。我会创建代码并在一段时间内给你
  • 非常感谢您的完美运行。我可以再问一件事吗?是否有可能有像 As 从 0 开始这样的表单字段,而用户将输入其余部分。 0(andusertypes10moredigits)
  • 是的,默认情况下可以只定义值 0,然后当用户输入其他 10 位数字时,您可以简单地连接该值。
  • 我真的不知道该怎么做,你能帮我编辑你的代码吗?再次感谢。
【解决方案2】:

你可以编写正则表达式来匹配你想要的格式,

    let regex = /[0]\d+/gi;
    let match = regex.exec(e);
    if(match && match.length == 11){

        return true; // match found with 11 digits number starting with 0
    }
    else{
    alert('invalid number'); // no match found
    }

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2011-12-07
    • 1970-01-01
    • 2022-11-22
    • 2014-04-01
    • 2012-08-11
    相关资源
    最近更新 更多