Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
联系人表单电话号码格式Javascript_Javascript - Fatal编程技术网

联系人表单电话号码格式Javascript

联系人表单电话号码格式Javascript,javascript,Javascript,这段代码允许我将电话号码以(XXX)XXX-XXXX格式写入我的联系人表单。(工作示例位于) 但我需要像0XXXXXXXXXX一样完成,第一个字符必须是0,不允许使用字母或任何其他字符 这是我头上的代码标签 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://unpkg.com/jquery-

这段代码允许我将电话号码以(XXX)XXX-XXXX格式写入我的联系人表单。(工作示例位于)

但我需要像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>

$(文档).ready(函数(){
$(“#您的手机”).usPhoneFormat({
格式:‘(xxx)xxx xxxx’,
});
$(“#yourphone2”).usPhoneFormat();
});
这是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);
            }
        });

    }
}
(函数($){
$.fn.usPhoneFormat=函数(选项){
变量参数=$.extend({
格式:“xxx xxx xxxx”,
国际:错,
},选项);
如果(params.format=='xxx xxx xxxx'){
$(this).bind('paste',函数(e){
e、 预防默认值();
var inputValue=e.originalEvent.clipboardData.getData('Text');
如果(!$.isNumeric(inputValue)){
返回false;
}否则{
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);
}
});
$(此).on('keypress',函数(e){
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
返回false;
}
var curchr=this.value.length;
var curval=$(this.val();
如果(curchr==3){
$(此).val(曲线+“-”);
}else if(curchr==7){
$(此).val(曲线+“-”);
}
$(this.attr('maxlength','12');
});
}否则如果(params.format=='(xxx)xxx xxxx'){
$(此).on('keypress',函数(e){
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
返回false;
}
var curchr=this.value.length;
var curval=$(this.val();
如果(curchr==3){
$(this.val(“(“+curval+”)“+”);
}else if(curchr==9){
$(此).val(曲线+“-”);
}
$(this.attr('maxlength','14');
});
$(this).bind('paste',函数(e){
e、 预防默认值();
var inputValue=e.originalEvent.clipboardData.getData('Text');
如果(!$.isNumeric(inputValue)){
返回false;
}否则{
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))

  • 只需将文本框定义为如下所示,该文本框只允许以0开头的11位数字
  • $(文档).ready(函数(){
    $(“#编号”)。按键(功能(e){
    if(this.value.length==0&&e.which!=48){
    返回错误
    }
    if(e.which<48 | | e.which>57 | | this.value.length>10){
    返回错误
    }
    });	
    });
    
    您可以编写正则表达式以匹配所需的格式

        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
        }
    

    该插件只支持两种手机格式。我建议你完全放弃它,从中学习并编写自己的版本。这个正则表达式行是键:
    inputValue=String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/,“($1)$2-$3”)您好,谢谢您的帮助,但这并没有限制最大数字。因此,您需要在此基础上添加按键事件并进行检查。我将创建代码,并将在某个时候给你非常感谢你这么多的作品完美。我能再问一件事吗?是否可以使用类似于起始0的表单字段,并由用户键入其余字段。0(和用户类型10个以上数字)是的,可以在默认情况下定义值0,然后当用户输入其他10个数字时,您可以简单地计算该值。我真的不知道怎么做,您能帮助我编辑您的代码吗?再次感谢。