javascript 如何启用在屏蔽输入插件(JQuery 插件)中插入空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24305200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:32:39  来源:igfitidea点击:

How to enable to insert spaces in masked input plugin (JQuery Plugin)?

javascriptjquerymask

提问by user3655719

I use masked input plugin, but it forbid enter spaces. * - Represents an alphanumeric character (A-Z,a-z,0-9), but not allow space-symbol

我使用屏蔽输入插件,但它禁止输入空格。* - 代表一个字母数字字符 (AZ,az,0-9),但不允许有空格符号

采纳答案by Iryna

In case if you use http://github.com/RobinHerbots/jquery.inputmask, you can create your own mask:

如果您使用http://github.com/RobinHerbots/jquery.inputmask,您可以创建自己的掩码:

$.extend($.inputmask.defaults.definitions, {
    'A': { 
        validator: "[A-Za-z0-9 ]",
        cardinality: 1
    }
});
$("#field").inputmask("AAA");

If you use http://plugins.jquery.com/maskedinput/, you create you mask like this:

如果您使用http://plugins.jquery.com/maskedinput/,您可以像这样创建掩码:

$.mask.definitions['A'] = "[A-Za-z0-9 ]";
$("#field").mask("AAA");

回答by Mr7-itsurdeveloper

you can change .mask and simply give space after *

您可以更改 .mask 并在 * 后简单地留出空间

//following script available in jquery.maskedinput.js
$.mask = {
    //Predefined character definitions
    definitions: {
        '9': "[0-9]",
        'a': "[A-Za-z]",
        '*': "[A-Za-z0-9]"
    },
    dataName: "rawMaskFn",
    placeholder: '_',
};

// if all ,then put *,if only number then put 9,if only alpha then put a

// 如果全部,则放 *,如果只有数字则放 9,如果只有 alpha 则放一个

check this:

检查这个:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
 <script type="text/javascript" src="http://cdn.jsdelivr.net/jquery.maskedinput/1.3.1/jquery.maskedinput.js"></script>
    <script>
     jQuery(function($)
        { 
        $.mask.definitions['~']='[+-]';    
        $('#Cell').mask('(*   **)-(99 9)  (a  aa)');        
        }); 


    </script>   

http://jsfiddle.net/2Xz8q/1/http://jsfiddle.net/2Xz8q/2/

http://jsfiddle.net/2Xz8q/1/ http://jsfiddle.net/2Xz8q/2/

回答by APP DEV

just need to input name with blank like this : name = blabla bla bla and this is my code :

只需要像这样输入带有空白的名称:name = blabla bla bla 这是我的代码:

  <label for="nom" class="col-md-4 control-label"><span class="text-danger">*</span>Nom</label>
    <script>
        $(document).ready(function(){
        $("#nom").inputmask("A", { repeat: 10 }, { "placeholder": "_" });
        });
    </script>

回答by Luis Daniel Rovira Contreras

in this way you add spaces the key is the \ s that in regular expressions means spaces greetings it is not advisable to put a space between the brackets because it causes a bug

通过这种方式,您添加空格键是 \ s,在正则表达式中表示空格问候语 不建议在括号之间放置空格,因为它会导致错误

$('#'+id).mask('ZZ',{translation: {'Z': {pattern: /[áéíóú?üàèa-zA-Z0-9\s]/, recursive: true}}});

$('#'+id).mask('ZZ',{translation: {'Z': {pattern: /[áéíóú?üàèa-zA-Z0-9\s]/, recursive: true}}});