验证十进制数字 [英] validate decimal numbers

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

问题描述

我想验证一个数字是否具有某些参数,例如,我想确保一个数字具有3个小数是正数.尽管找不到方法,但我在Internet上的不同位置进行了搜索.我已经使该文本框仅接受数字.我只需要其余的功能.

I want to validate that a number has certain parameters, for example I want to ensure that a number has 3 decimals is positive. I have searched in different places over the internet, although I could not find how to do it. I have made that text box to accept numbers only. I just need the rest of the features.

谢谢

$("#formEntDetalle").validate({
                    rules: {

                        tbCantidad: { required: true, number: true },
                        tbPrecioUnidad: { required: true, number: true },

                    }
                    messages: {

                        tbCantidad: { required: "Es Necesario Entrar una cantidad a la orden" },
                        tbPrecioUnidad: { required: "Es Necesario Entrar el valor valido para el producto" }

                    },
                    errorPlacement: function(error, element) {
                        parent = element.parent().parent();
                        errorPlace = parent.find(".errorCont");
                        errorPlace.append(error);
                    }
                });

我想通过以下方式控制该文本框:

I want to control that text box with something like:

$.validator.addMethod('Decimal',
                    function(value, element) {
                       //validate the number
                    }, "Please enter a correct number, format xxxx.xxx");

推荐答案

基于示例这里:

$.validator.addMethod('Decimal', function(value, element) {
    return this.optional(element) || /^\d+(\.\d{0,3})?$/.test(value); 
}, "Please enter a correct number, format xxxx.xxx");

或允许使用逗号:

$.validator.addMethod('Decimal', function(value, element) {
    return this.optional(element) || /^[0-9,]+(\.\d{0,3})?$/.test(value); 
}, "Please enter a correct number, format xxxx.xxx");

这篇关于验证十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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