简体   繁体   English

我正在尝试使用jquery和javascript将用户输入限制为有效价格。 如何防止第二个小数点

[英]I'm trying to limt user input to a valid price using jquery & javascript. How do i prevent second decimal point

I'm trying to limit my user input to conform to 999.99 我试图限制用户输入以符合999.99

I am using this code to try and exclude the entry of a second decimal point but it doesn't work. 我正在使用此代码尝试排除第二个小数点的输入,但它不起作用。 Can anyone help me out on this ? 有人可以帮我吗?

jQuery(".textr2").live('input', function (event) {

     var str = jQuery(this).val();

     alert(str);

     if (str.search('.')) {
        alert("Finds a dot");
        jQuery(this).val(jQuery(this).val().replace(/[^0-9]/g, ''));
     }
     else {
        alert("NO Dot");
        jQuery(this).val(jQuery(this).val().replace(/[^0-9\.]/g, ''));
    });

You could try checking the value enter on keypress. 您可以尝试检查按键上输入的值。

Fiddle Demo 小提琴演示

Try 尝试

$("#test1").keypress(function(event) 
{
    var numberPattern     = /^([0-9]{0,3})(\.[0-9]{0,2})?$/;    
    var key               = event.which;
    var character         = String.fromCharCode(key);
    var selectionStart    = this.selectionStart;
    var value             = $(this).val();
    var stringPattren     = new RegExp('(.{'+selectionStart+'})');
    var replacePattren    = '$1'+character;
    var string            = value.replace(stringPattren,replacePattren);
    var isTab             = key === 0;
    var isBackspace       = key === 8;
    var shouldTest        = ! isBackspace && ! isTab;
    var passed            =  ! shouldTest;

    if(shouldTest)
    {
       passed = numberPattern.test(string);
    }

    return passed;
});
var str        = this.value;
var firstDot   = str.indexOf('.');
var secondDot  = str.indexOf('.', firstDot + 1);

if ( firstDot >= 0 && secondDot >= 0 ) {
    this.value = str.substr(0, secondDot) + str.substr(secondDot + 1);
}

Here's the fiddle: http://jsfiddle.net/YN4ta/ 这是小提琴: http : //jsfiddle.net/YN4ta/

暂无
暂无

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

相关问题 如何使用 javascript 打开模态。 不使用 jquery - how do i open modal with javascript. without using jquery 我正在尝试使用JavaScript验证用户输入。 我想验证用户是否正确输入了他们的课程名称 - I am trying to validate user input using JavaScript. I would like to validate that the user has input their the title of their course correctly 我尝试在 javascript 中创建一个 while 循环。 我有我想在身体上做什么的例子 - I try to make a while loop in javascript. I have example of what i'm trying to do in body 如何使用javascript / jquery防止在HTML文本字段中键入小数点? - How can I use javascript/jquery to prevent a decimal point from being typed into an HTML text field? 如何使用javascript从网站提取数据。 - How do I extract data from a website using javascript. 如何防止小数点重复? - How do I prevent the decimal point from repeating? 我正在用 Javascript 创建一个二维数组。 如何跳过创建它的一部分? - I'm creating a 2D array in Javascript. How do I skip creating a part of it? 如何在Javascript中获取浮点数的小数位? - How do I get the decimal places of a floating point number in Javascript? 如何使用 JavaScript 获得多个 model 图像弹出窗口。 我正在使用 W3School model 图像 - How I can I get multiple model image popups using JavaScript. I'm using W3School model image 尝试使用CSS(活动)和JavaScript在点击时显示内容。 如何保持内容显示? - Trying to display content on click using css (active) and javascript. How can I keep the content displaying?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM