如何检查大于0或小数点后两位?

时间:2017-11-07 05:35:29

标签: asp.net asp.net-mvc asp.net-mvc-5

在我的控制器中,我有一个if功能,如下面的检查

    char *a, *b, *c;

    printf("%s\n", argv[0]); 

    a = argv[1]; 
    b = argv[2]; 
    c = argv[3]; 

    int num1, num2;  
    num1 = convertToInt(a);
    num2 = convertToInt(c);


    switch (*b) 
    {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case 'x':
            result = num1 * num2;
            break;
        case '/':
            result = num1 / num2;
            break;
        case '%':
            result = num1 % num2;
            break;
    }

}


 printf( "%d\n", result );

有效输入

(model.products.ProductTypeID > 0 && categoryIdList != "" &&
 model.products.ProductName != null && model.products.SellingPrice > 0 && model.products.BasicPrice > 0 && model.products.BuyingPrice > 0
 && model.products.ReorderQuantity > 0 && model.products.ReorderLevel > 0)

无效输入

2
2.2
2.22

我怎样才能检查售价0或者它有两个小数点,只要它有3个它不起作用

1 个答案:

答案 0 :(得分:0)



 $(function () {
        $('.number-only').keypress(function (event) {
            var $this = $(this);
            if ((event.which != 46 || $this.val().indexOf('.') != -1) &&
               ((event.which < 48 || event.which > 57) &&
               (event.which != 0 && event.which != 8))) {
                event.preventDefault();
            }

            var text = $(this).val();
            if ((event.which == 46) && (text.indexOf('.') == -1)) {
                setTimeout(function () {
                    if ($this.val().substring($this.val().indexOf('.')).length > 3) {
                        $this.val($this.val().substring(0, $this.val().indexOf('.') + 3));
                    }
                }, 1);
            }

            if ((text.indexOf('.') != -1) &&
                (text.substring(text.indexOf('.')).length > 4) &&
                (event.which != 0 && event.which != 8) &&
                ($(this)[0].selectionStart >= text.length - 4)) {
                event.preventDefault();
            }
        });

        $('.number-only').bind("paste", function (e) {
            var text = e.originalEvent.clipboardData.getData('Text');
            if ($.isNumeric(text)) {
                if ((text.substring(text.indexOf('.')).length > 3) && (text.indexOf('.') > -1)) {
                    e.preventDefault();
                    $(this).val(text.substring(0, text.indexOf('.') + 3));
                }
            }
            else {
                e.preventDefault();
            }
        });
        //$('.number-only').keypress(function (e) {

        //    if (isNaN(this.value + "" + String.fromCharCode(e.charCode))) return false;


        //})
        //.on("cut copy paste", function (e) {
        //    e.preventDefault();
        //});
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <input name="Tsm_Rate" id="Pr" placeholder="Price" class="form-control number-only" type="text"/>
&#13;
&#13;
&#13;

你可以使用j查询来做到这一点:_这里你只能输入十进制后的四位数,直到你得到小数点后你想要多少位数