实时控制带有正则表达式掩码的浮点输入 [英] Live control a float input with a regex mask

查看:78
本文介绍了实时控制带有正则表达式掩码的浮点输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个实时控制数字和浮点数字的功能. 但是它对于浮点数不能正常工作,必须像这样的表达式:

// I wish a number like x figures . 3 figures (example : 123456.123)
/^([1-9][0-9]*|0)(\.[0-9]{3})?$/

但是这个表达式使点消失了……

以下方法有效/(^\d+$)|(^\d+\.\d+$)|[,.]/,但可以添加多个点:

$('.float_input').live("keypress",function(){inputControl($(this),'double');});

function inputControl(input,format) 
{ 
    var value=input.val();
    if (format=='int'){expression=/(^\d+$)|(^\d+\.\d+$)/;}
    else if (format=='double'){expression=/(^\d+$)|(^\d+\.\d+$)|[,.]/;}
    var values=value.split("");
    var update="";
    for(id in values)
    {           
        if (expression.test(values[id])==true && values[id]!='')
        {
              // also replace ',' by '.' 
              update=update+''+values[id].replace(',','.');
        }
    }
    input.val(update);
}

所以我有多个点或没有点,这让我很生气,因为我敢肯定会接近解决方案... 预先感谢您的帮助.

编辑>解决方案

非常感谢您对正则表达式的帮助,我找到了解决方案!

两个测试是必要的:

  • 一个人进行角色测试,一个接一个地测试
  • 另一个在输入角色时测试整个输入的

这是最终的脚本,它的工作原理就像一朵花,我专门为您分享:

$('.numeric_input').live("keyup",function(){inputControl($(this),'int');});
$('.float_input').live("keyup",function(){inputControl($(this),'float');});

function inputControl(input,format) 
{ 
    var value=input.val();
    var values=value.split("");
    var update="";
    var transition="";
    if (format=='int'){
        expression=/^([0-9])$/;
        finalExpression=/^([1-9][0-9]*)$/;
    }
    else if (format=='float')
    {
        var expression=/(^\d+$)|(^\d+\.\d+$)|[,\.]/;
        var finalExpression=/^([1-9][0-9]*[,\.]?\d{0,3})$/;
    }   
    for(id in values)
    {           
        if (expression.test(values[id])==true && values[id]!='')
        {
            transition+=''+values[id].replace(',','.');
            if(finalExpression.test(transition)==true)
            {
                update+=''+values[id].replace(',','.');
            }
        }
    }
    input.val(update);
}

解决方案

此正则表达式

(/(^ \ d + $)|(^ \ d +.\ d + $)| [,.]/)

应该匹配

  • 每(^ \ d + $)1111111
  • 或每((^ \ d +.\ d + $))111111.11111
  • 或逗号后跟任何字符,它可以在表达式中的任何位置.

我怀疑您的正则表达式应该是 请注意,我已经摆脱了最后一个时期.可以匹配逗号或句点

/(^ \ d + [,\.] {0,1} \ d {3})/

根据注释中的说明,可能正是您想要的

[-+]?[0-9] * \.?[0-9] +

也可以工作

注意:您可以使用Roy Osherove的 Regulazy 或Regex Buddy工具来极大地简化正则表达式的生活. >

I've made a function to live control numbers and float numbers. But it doesn't work properly for float numbers, wich have to be like this expression :

// I wish a number like x figures . 3 figures (example : 123456.123)
/^([1-9][0-9]*|0)(\.[0-9]{3})?$/

But this expression makes desappear the dot...

The follow works /(^\d+$)|(^\d+\.\d+$)|[,.]/, but multiple dots can be added :

$('.float_input').live("keypress",function(){inputControl($(this),'double');});

function inputControl(input,format) 
{ 
    var value=input.val();
    if (format=='int'){expression=/(^\d+$)|(^\d+\.\d+$)/;}
    else if (format=='double'){expression=/(^\d+$)|(^\d+\.\d+$)|[,.]/;}
    var values=value.split("");
    var update="";
    for(id in values)
    {           
        if (expression.test(values[id])==true && values[id]!='')
        {
              // also replace ',' by '.' 
              update=update+''+values[id].replace(',','.');
        }
    }
    input.val(update);
}

So I have multiple dots or no dot, it makes me nut because I'm sure to be near of the solution... Thanks in advance for your help.

EDIT > SOLUTION

Ouch, thanks for the help about regex, I've found the solution !

Two tests were necessary :

  • one for the caracters test, tested one by one
  • another to test the entire input while entering caracters

This is the final script, wich works like a flower, and I share it just for you :

$('.numeric_input').live("keyup",function(){inputControl($(this),'int');});
$('.float_input').live("keyup",function(){inputControl($(this),'float');});

function inputControl(input,format) 
{ 
    var value=input.val();
    var values=value.split("");
    var update="";
    var transition="";
    if (format=='int'){
        expression=/^([0-9])$/;
        finalExpression=/^([1-9][0-9]*)$/;
    }
    else if (format=='float')
    {
        var expression=/(^\d+$)|(^\d+\.\d+$)|[,\.]/;
        var finalExpression=/^([1-9][0-9]*[,\.]?\d{0,3})$/;
    }   
    for(id in values)
    {           
        if (expression.test(values[id])==true && values[id]!='')
        {
            transition+=''+values[id].replace(',','.');
            if(finalExpression.test(transition)==true)
            {
                update+=''+values[id].replace(',','.');
            }
        }
    }
    input.val(update);
}

解决方案

This regex

(/(^\d+$)|(^\d+.\d+$)|[,.]/)

should match

  • 1111111 per the (^\d+$)
  • or 111111.11111 per the (^\d+.\d+$)
  • or the comma followed by any character, and it could be anywhere in the expression.

I'm suspecting your regex should be Note that I've escaped the final period. That would match a comma or a period

/(^\d+[,\.]{0,1}\d{3})/

may be exactly what you want based on clarifications in the comments

[-+]?[0-9]*\.?[0-9]+

would also work

NOTE: You can simplify your regex life tremendously by using Roy Osherove's Regulazy or the tool Regex Buddy.

这篇关于实时控制带有正则表达式掩码的浮点输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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