简体   繁体   English

两个或多个点的正则表达式

[英]Regular expression for two or more dot

live: http://jsfiddle.net/2KRHh/3/ 直播: http//jsfiddle.net/2KRHh/3/

$('#price').keyup(function(){
        $('#price').val($('#price').val().replace('..', '.'));
})

I would like have only one dot in input. 我只想输入一个点。 This working, but only for .. digit. 这有效,但仅适用于..数字。 If i still hold comma this not working, so i would like replace this with regular expression - if is two or more dot this should replace one more. 如果我仍然按住逗号不起作用,那么我想用正则表达式替换它-如果是两个或多个点,则应该替换一个。

This should also remove dot if is in different place - for example 如果位于其他位置,也应该删除点-例如

11.23.32 - should remove second dot.

If you want to leave the first period alone and remove the others, the expression is simple: 如果您想单独留下第一个句点并删除其他句点,则表达式很简单:

/\./g

It basically matches all periods; 它基本上匹配所有时段; so how are we making sure to only match the second, third, etc.? 那么我们如何确保只匹配第二,第三等等? Simple, you manage that in a replacement function: 很简单,您可以通过替换功能进行管理:

var $price = $('#price'),
new_str = function(str) {
    var i = 0;

    return str.replace(/\./g, function() {
        return ++i >= 2 ? '' : '.';
    });
}($price.val());

$price.val(new_str);

The variable i is used to keep track of how many times the period has been matched; 变量i用于跟踪时间段已匹配了多少次; if it has matched two times or more, it returns an empty string and effectively removes the period. 如果匹配两次或更多次,则返回一个空字符串并有效地删除句点。

Change the event to keydown and stop the event if the keypress is a dot and there is already at least one dot in the value. 如果按键是一个点并且值中已经至少有一个点,则将事件更改为keydown并停止事件。

$('#price').keydown( function(e) {
    if( e.which === 190 && $(this).val().indexOf( '.' ) !== -1 ) {
        return false;
    }
});

Demo: http://jsfiddle.net/2KRHh/9/ 演示: http//jsfiddle.net/2KRHh/9/

And to give you another choice..... 并给您另一个选择。

$('#price').keyup(function(){
    $('#price').val($('#price').val().replace(/([^.]*\.[^.]*)\./,'$1'));
   })

You should NEVER restrict the user from what they want to type. 您绝对不应限制用户输入他们想要输入的内容。 When validating, you should show some kind of warning or auto-correct it AFTER they are done typing. 验证时,您应该在输入完毕后显示某种警告或对其进行自动更正。

Therefore, try this: 因此,请尝试以下操作:

<input type="number" min="0" step="0.01" />

Because, amazingly, browsers are bloody powerful systems that don't have to be spoon-fed everything ;) 因为,令人惊讶的是,浏览器是强大的强大系统,不必一事无成;)


However, if compatibility is a major concern (note: it shouldn't be, because you should never trust client input anyway and always validate on the server), try this: 但是,如果兼容性是主要问题(请注意:不应该这样,因为无论如何您都不应该信任客户端输入,并且始终在服务器上进行验证),请尝试以下操作:

<input type="number" min="0" step="0.01"
 onChange="this.value=Math.max(0,Math.floor((parseFloat(this.value)||0)*100)/100);" />

I don't know if regular expressions are the best way to go about this. 我不知道正则表达式是否是解决此问题的最佳方法。

http://jsfiddle.net/2KRHh/4/ http://jsfiddle.net/2KRHh/4/

Or the relevant code: 或相关代码:

var str = $('#old').val();
var dot = str.indexOf('.');
var newstr = str.substring(0, dot) + '.' + str.substring(dot).replace('.', '', 'g');
console.log(str, newstr);                       
$('#new').val(newstr);

The code finds the first dot and saves its position, then replaces the rest of the dots with empty string. 该代码找到第一个点并保存其位置,然后用空字符串替换其余的点。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM