简体   繁体   English

仅接受数字和小数

[英]Accept only numbers and decimal

Hello im creating a directive in angularjs that only accepts positive numbers and decimal, and other charecters that arent numbers (ex: +, -, abc...) will me removed. 您好,我在angularjs中创建了一个指令,该指令仅接受正数和十进制,以及其他将不删除任何数字(例如:+,-,abc ...)的字符。

First i tried using: 首先我尝试使用:

var transformedInput = text.replace(/[^0-9]/g, '');

And it works but doesnt accept decimal numbers, so i use: 它有效,但不接受十进制数字,因此我使用:

transformedInput = text.replace(/[^0-9]+([,.][0-9]+)?$/g, '');

What im doing wrong in my code? 我在我的代码中做错了什么? can someone check it please. 有人可以检查一下吗。

Can you simply remove non numbers, non decimal point, non newlines, and non comma? 您可以简单地删除非数字,非小数点,非换行符和非逗号吗? If so you can use a super simple regex: 如果是这样,您可以使用超级简单的正则表达式:

[^0-9.,\n]+  //global, multiline, replace with blank string

demo 演示

/^[0-9]*$/

^ : Start on the begin. ^ :从头开始。

[0-9]* : All number. [0-9]* :所有数字。

$ : Finish at the end. $ :最后结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM