简体   繁体   English

输入仅允许小数点后一位

[英]Input to allow for only one decimal point

I am trying to format a number upon input, where you can only insert valid numbers. 我正在尝试在输入时格式化数字,在此只能插入有效数字。 I have everything working except for the decimal. 除小数点外,我一切正常。 The input box allows me it insert as many decimals as I would like, but I would only like to allow for one (see the last replace() ). 输入框允许我插入任意数量的小数,但我只想允许一个小数(请参阅最后一个replace() )。

element.oninput = e => {
    let value = e.currentTarget.value;
    let result = value
        // Remove anything that isn't valid in a number
        .replace(/[^0-9-.]/g, '')
        // Remove all dashes unless it is the first character
        .replace(/(?!^)-/g, '')
        // Remove all periods unless it is the last one
        .replace(/(?!)\./g, '');
    element.value = result;
}

https://jsfiddle.net/c4f1L3kv/1/ https://jsfiddle.net/c4f1L3kv/1/

Here are some valid inputs: 以下是一些有效的输入:

123.123
-123.12
123
-123
.123
-.123

Here are some invalid inputs: 以下是一些无效的输入:

123-123
1-2-3
123.123.123
123-123..12

 var element = document.querySelector('input'); element.oninput = e => { let value = e.currentTarget.value; let result = value .replace(/[^0-9-.]/g, '') .replace(/(?!^)-/g, '') // prevent inserting dots after the first one .replace(/([^.]*\\.[^.]*)\\./g, '$1'); element.value = result; } 
 <input/> 

If you only want to match a period character if it is followed by another period character, then you can use a positive lookahead like in the expression below: 如果您只想在一个句点字符后接另一个句点字符,则可以使用正向前瞻,例如下面的表达式:

/\.(?=.*\.)/g

Explanation: 说明:

  • \\. - Match a literal . -匹配文字. character 字符
  • (?= - Start of a positive lookahead (?= -开始积极的前瞻
    • .*\\. - Match zero or more characters until a literal . -匹配零个或多个字符,直到一个文字. character. 字符。
  • ) - Close of the positive lookahead ) -正向提前结束

 var element = document.querySelector('input'); element.addEventListener('input', (event) => { event.target.value = event.target.value // Remove anything that isn't valid in a number .replace(/[^\\d-.]/g, '') // Remove all dashes unless it is the first character .replace(/(?!^)-/g, '') // Remove all periods unless it is the last one .replace(/\\.(?=.*\\.)/g, ''); }); 
 <input type="text" /> 


Based on your comment below: 根据您的以下评论:

If you would like to prevent the user from adding a period character at the end of the string if a period character is already present, then you could use the expression /(\\..*)\\.$/ and replace the first capturing group with itself, which will effectively remove whatever wasn't in the capturing group (ie, the last period character). 如果要阻止用户在字符串末尾添加句点字符(如果已经存在句点字符),则可以使用表达式/(\\..*)\\.$/并替换第一个捕获组本身,它将有效地删除捕获组中未包含的所有内容(即最后一个句号)。

 var element = document.querySelector('input'); element.addEventListener('input', (event) => { event.target.value = event.target.value // Remove anything that isn't valid in a number .replace(/[^\\d-.]/g, '') // Remove all dashes unless it is the first character .replace(/(?!^)-/g, '') // Remove the last period if there is already one .replace(/(\\..*)\\.$/, '$1') // Remove all periods unless it is the last one .replace(/\\.(?=.*\\.)/g, ''); }); 
 <input type="text" /> 

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

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