简体   繁体   English

屏蔽输入以仅允许十六进制颜色

[英]Masked input to allow only hex colors

using the jQuery Validate plugin or maybe manually with jQuery, I'd like to create an input field which accepts values with a '#' sign at the beginning, and 6 other characters.使用 jQuery Validate 插件或手动使用 jQuery,我想创建一个输入字段,该字段接受开头带有“#”符号的值和其他 6 个字符。 So, the input would accept something like this:因此,输入将接受如下内容:

#fff, #ff0000, #f00f , but would decline '1232131231', or something else that doesn't match with the mask. #fff, #ff0000, #f00f ,但会拒绝“1232131231”或其他与掩码不匹配的内容。 I hope someone can help me with this.我希望有人能帮我解决这个问题。

没有办法让它只接受 # 开箱即用,但你可以做的是让它只接受数字,然后在提交后将 # 添加到开头。

Try this:尝试这个:

<input type="text" id="color">

$(document).ready(function() {
    $('#color').on('keyup', function() {
        if (/^#\d{6}$/.test($(this).val())) {
            console.log("it's a hex color");
        } else {
            console.log("it's not a hex color");
        }
    });
});

Working fiddle: http://jsfiddle.net/QEsGY/工作小提琴: http : //jsfiddle.net/QEsGY/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jquery Masked Input插件允许点 - Jquery Masked Input plugin allow dot 仅接受十六进制输入 - Accept hex only input 如何使用单元格的应用程序脚本将 Google 表格数据验证添加到列中,以仅允许输入颜色和十六进制颜色代码 - How do I add Google sheets data vaildation to a column using apps script for cells to only allow colors and hex color codes to be inputted 仅显示与屏蔽输入等效的内容(javascript) - Display only equivalent to masked input (javascript) 随机十六进制发生器(仅灰色) - Random Hex generator (only grey colors) 获取颜色输入名称(而不是十六进制) - 颜色数据列表 - Getting color input name (instead of HEX) - datalist of colors 如何制作仅允许特定数字的掩码输入? - How do I make a masked input that only allows specific numers? jquery蒙面输入只有第一个数字可选的rest必须 - jquery masked input have only first number optional rest mandatory 输入如何仅接受 HEX 数字 - How input accepts only HEX numbers 如何为允许字母数字、空格和重音字符的 jQuery 掩码输入插件创建掩码? - How to create a mask for jQuery Masked Input Plugin that allow alphanumeric, spaces and accented characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM