使用javascript或jquery在文本字段中进行十进制验证 [英] Decimal validation in the textfield using javascript or jquery

查看:83
本文介绍了使用javascript或jquery在文本字段中进行十进制验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证 keyup 事件中的文本字段.

i Want to validate a text field in keyup event .

在该字段中,它应接受货币类型的小数,如

in the field it should accept money type decimal like

(12.23) (.23) (0.26) (5.09) (6.00)

(12.23) (.23) (0.26) (5.09) (6.00)

如果我输入了错误的值,那么它应该返回到先前的值并删除错误的值

if i enter some wrong value then it should return to the previous value and remove the wrong one

推荐答案

我认为这样可能是最好的选择

I think something like this might be your best bet

var isValidCurrency = function(str) {
  var num = parseFloat(str);
  return !Number.isNaN(num) && num.toFixed(2).toString() === str;
};

一些测试

isValidCurrency("1234.56");   // true
isValidCurrency("1234.565");  // false
isValidCurrency("1234");      // false
isValidCurrency("foo");       // false

这篇关于使用javascript或jquery在文本字段中进行十进制验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆