在输入字段中设置最小金额

时间:2023-01-27 17:28:59

I have this input field where I want to add the minimum amount for the user to enter if minimum amount is Rs. 100, so the user should not enter amount less than that. I tried using min but it still getting redirected to other page on submit. Also I am restricting the user to enter first digit as 0. The script validation for numeric value is working fine. But the problem is user can not type the number with the right side key available in the keyboard. Below is the code I have tried so far. Please help me find my mistake.

我有这个输入字段,如果最小金额是Rs,我想添加用户输入的最小金额。 100,所以用户不应该输入少于该数量的金额。我尝试使用min但仍然会在提交时重定向到其他页面。此外,我限制用户输入第一个数字为0.数值的脚本验证工作正常。但问题是用户无法使用键盘中的右侧键输入数字。下面是我到目前为止尝试过的代码。请帮我找出错误。

HTML CODE:

<input type="text" class="input_style" maxlength="6" min="100" id="stamp_amount" name="answer[28]" placeholder='Minimum Rs. <%= @service.min_stamp %> ' required/>

JS CODE:

    $("#stamp_amount").keydown(function (e) {
       // Allow: backspace, delete, tab, escape, enter and .
       if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
         // Allow: Ctrl+A, Command+A
        (e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) || 
         // Allow: home, end, left, right, down, up
        (e.keyCode >= 35 && e.keyCode <= 40)) {
             // let it happen, don't do anything
             return;
       }

       if ( event.keyCode == 46 || event.keyCode == 8) {
        // let it happen, don't do anything

       }
       else {
        // Ensure that it is a number and stop the keypress
          if ((event.keyCode !==9) && (event.keyCode < 48 || event.keyCode > 57 )) {
            event.preventDefault(); 
        }   
        else{

          if($.trim($(this).val()) =='')
        {
            if(event.keyCode == 48){
            event.preventDefault(); 
            }
        }

        }
    }
});

2 个解决方案

#1


0  

Did you tried HTML5 input type number?

你试过HTML5输入类型号吗?

<input type="number" value="100" min="100"/>

http://www.w3schools.com/html/html_form_input_types.asp

#2


0  

Try something like this:

尝试这样的事情:

 $(document).keydown(function (e) {console.log(e.keyCode)});

Just to see if the keyCodes are the same, for me they are.

只是看看keyCodes是否相同,对我来说就是这样。

#1


0  

Did you tried HTML5 input type number?

你试过HTML5输入类型号吗?

<input type="number" value="100" min="100"/>

http://www.w3schools.com/html/html_form_input_types.asp

#2


0  

Try something like this:

尝试这样的事情:

 $(document).keydown(function (e) {console.log(e.keyCode)});

Just to see if the keyCodes are the same, for me they are.

只是看看keyCodes是否相同,对我来说就是这样。