最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery regex test - Stack Overflow

programmeradmin4浏览0评论

I'm trying to validate a value against a regex to check if the number is either a decimal (with only 2 digits after the decimal point)/integer. The rest should be invalid.

I used the suggested regex here: Simple regular expression for a decimal with a precision of 2

But when I included it in my jquery regex function, it doesn't seem to work:

    function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass( "ui-state-error" );
            updateTips( n );
            return false;
        } else {
            return true;
        }
    }

bValid = bValid && checkRegexp( o_buy, /\d+(\.\d{1,2})?/, "Buy field can only contain numbers, no currency please" );

What am I doing wrong?

Thanks,

I'm trying to validate a value against a regex to check if the number is either a decimal (with only 2 digits after the decimal point)/integer. The rest should be invalid.

I used the suggested regex here: Simple regular expression for a decimal with a precision of 2

But when I included it in my jquery regex function, it doesn't seem to work:

    function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass( "ui-state-error" );
            updateTips( n );
            return false;
        } else {
            return true;
        }
    }

bValid = bValid && checkRegexp( o_buy, /\d+(\.\d{1,2})?/, "Buy field can only contain numbers, no currency please" );

What am I doing wrong?

Thanks,

Share Improve this question edited May 23, 2017 at 12:29 CommunityBot 11 silver badge asked Feb 26, 2011 at 10:38 Or WeinbergerOr Weinberger 7,49223 gold badges72 silver badges117 bronze badges 2
  • Where does bValid e from? If it is false, the check function will never be executed – Pekka Commented Feb 26, 2011 at 10:38
  • bValid is true in this example. – Or Weinberger Commented Feb 26, 2011 at 10:41
Add a ment  | 

3 Answers 3

Reset to default 3

You'll probably want to make that regex a little more precise, /^-?\d+(\.\d{1,2})?$/. Otherwise, it'll only test that the string contains a number.

Edit: updated the regex to cover negative numbers.

What do you mean by “it does not seem to work”? I've just tested your example, and it works as intended.

The sole problem is that you try to find the numeric pattern anywhere inside the string. It means that something like abc3.4def is accepted by your regexp.

However I suppose that you want the string to contain only a decimal number? Then you have to add the ^ and $ symbols at the start and at the end, to tell test that you want to match the entire string. So the regexp bees:

/^\d+(\.\d{1,2})?$/

Was this your issue? If not, I may post a plete webpage that works OK for me, if it can be of any help to you.

I'm pretty sure you want to be using the following regex

/^\d+(\.\d{1,2})?$/

The problem with what you have is that {1,2} matches between 1 to 2 digits after the decimal, but doesn't fail if there is more. This is expected behavior.

发布评论

评论列表(0)

  1. 暂无评论