开发者

I have Input text box which should take $ and £ currency symbols and warn if user types any other special characters

开发者 https://www.devze.com 2023-04-03 00:49 出处:网络
For instance, $1.50, 1.50, £4, 3.4 should be processed successfully. Where as just $ or % or # or !2.4 or &45 should given an alert ( special characters ) .

For instance, $1.50, 1.50, £4, 3.4 should be processed successfully. Where as just $ or % or # or !2.4 or &45 should given an alert ( special characters ) .

Code: ( in JS )

 validatePrice : function(price){
        var regexPattern = '^(\\$)\[0-9]+(\.\?[0-9]+)?$';
        if (document.asset_edit_frm.price.value.search(regexPattern)== -1){
            alert('Not a valid currency format ');
            docume开发者_如何学Cnt.asset_edit_frm.price.focus();
            return false;
        }else return true;
    }


This will match the pattern you described.

^(?:\$|\u00A3)?[0-9]+(?:\.(?=[0-9]))?[0-9]*$

By the way, if this is straight JavaScript, define the regex as follows:

var regexPattern = /^(?:\$|\u00a3)?[0-9]+(?:\.(?=[0-9]))?[0-9]*$/;

Note the lack of '


checking for numbers can be done easily with this jquery plugin

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号