Javascript 正则表达式只允许数字和$symbol

Javascript 正则表达式只允许数字和$symbol,javascript,html,regex,Javascript,Html,Regex,我有一个要求,用户只能在文本框中输入数字和$符号。不应允许任何其他操作,并且应显示警报消息 ^[\d\$]+$ 要在HTML输入验证中使用此正则表达式,请使用正则表达式 [\d\$]+ 说明: ^ assert position at start of the string [\d\$]+ match a single character present in the list below Quantifier: + Between one and unlimited times, as m

我有一个要求,用户只能在文本框中输入数字和
$
符号。不应允许任何其他操作,并且应显示警报消息

^[\d\$]+$
要在HTML输入验证中使用此正则表达式,请使用正则表达式

[\d\$]+
说明:

^ assert position at start of the string
[\d\$]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
\$ matches the character $ literally
$ assert position at end of the string
例如:

$(function(){
    $('#text').keyup(function(){
        val = $(this).val();
        res = val.match(/^[\d\$]+$/);
        if(res == null){
            alert("Enter only $ or numeric");
        }
    })
})

尝试以下功能

function AllowNumbers(e, field) {
var val = field.value;
var re1 = /(^[0-9$]+)/g;

val = re1.exec(val);
if (val) {
    field.value = val[0];
} else {
    field.value = "";
}
}

如果OP的目的是使用HTML5验证功能,使用
模式
属性,则在文本框键控时调用此函数,然后,
^
$
是隐式的,不是必需的。谷歌
html表单验证
。那么用户输入一个无效字符的那一刻,你就要清除他输入的所有内容了?为什么你有两个^?认为你的正则表达式有输入错误;第二个
^
应该在character类中。