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
精彩评论