正则表达式只允许数字和$符号

时间:2015-08-24 12:32:02

标签: javascript html regex

我有一个要求,即用户只能在文本框中输入数字和:符号。不应该允许任何其他内容,它应该显示警告信息。

2 个答案:

答案 0 :(得分:0)

^[\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");
        }
    })
})

DEMO

答案 1 :(得分:0)

尝试以下功能

<script>
        $(document).ready(function() {
        if(localStorage.getItem('welcomehelloState') != 'shown'){
        function openColorBox(){
                $.colorbox({transition: "fade", top: "2%", width:"67%", height:"17%", html: "<section class="related">
    <p>New Content:-</p>
    <a href="/part-2/index>
        <img src="img/related/Prdha.png" />
        <h3>Art</h3>
    </a>
    <a href="/part-2/index6">
        <img src="img/related/Kavi.png" />
        <h3>Games</h3>
    </a>
    </section>"});
                               }
        setTimeout(openColorBox, 100);
        localStorage.setItem('welcomehelloState','shown')
        };});
</script>

并在文本框的键盘上调用此函数