[英]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.