我想要获取仅为正数的值,是否有仅使用HTML的方法来防止输入负数?
请不要建议验证方法。
请不要建议验证方法。
按照以下方式使用min
属性:
<input type="number" min="0">
我找到的解决方案是:
<input type="number" min="0" oninput="this.value = Math.abs(this.value)">
根据评论中的建议进行了微小的更改,以使其在最小值为0的情况下仍能工作。
<input type="number" min="0" oninput="this.value =
!!this.value && Math.abs(this.value) >= 0 ? Math.abs(this.value) : null">
<input ng-model="row.myValue" type="{{row.code == 1 ? 'text' : 'number'}}" min="0" ng-pattern="..." noformvalidate oninput="if (this.type=='text') this.value=Math.abs(this.value) ">
。 - sebiusthis.value = Math.abs(this.value) > 0 ? Math.abs(this.value) : null
。 - ali6p我对@Abhrabm的回答不满意,因为:
它只能防止从上 / 下箭头输入负数,而用户可以通过键盘输入负数。
解决方法是使用按键代码来防止:
// Select your input element.
var number = document.getElementById('number');
// Listen for input event on numInput.
number.onkeydown = function(e) {
if(!((e.keyCode > 95 && e.keyCode < 106)
|| (e.keyCode > 47 && e.keyCode < 58)
|| e.keyCode == 8)) {
return false;
}
}
<form action="" method="post">
<input type="number" id="number" min="0" />
<input type="submit" value="Click me!"/>
</form>
@Hugh Guiney 提供的澄清:
正在检查哪些按键代码:
因此,该脚本防止无效的按键被输入。
min="0"
结合使用可能就足够了。 - Hugh Guiney|| (e.keyCode>36 && e.keyCode<41)
这将防止用户通过上下箭头键来增加或减少数字,并防止用户使用左右箭头键编辑数字。 - vusan这段代码在我的环境中可以正常工作。你能否检查一下:
<input type="number" name="test" min="0" oninput="validity.valid||(value='');">
<input type="number" name="test" min="0" oninput="validity.valid||(value=value.replace(/\D+/g, ''))">
- 这会删除所有非数字符号。 - Oleh Melnyk简单方法:
<input min='0' type="number" onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57">
我希望允许输入小数,如果输入了负数,不会清除整个输入内容。至少在谷歌浏览器中可以正常工作:
<input type="number" min="0" onkeypress="return event.charCode != 45">
keypress
是唯一的方式,可以在输入框中输入负数吗? - Roko C. Buljan以下是Angular 2的解决方案:
创建一个名为OnlyNumber的类:
import {Directive, ElementRef, HostListener} from '@angular/core';
@Directive({
selector: '[OnlyNumber]'
})
export class OnlyNumber {
// Allow decimal numbers. The \. is only allowed once to occur
private regex: RegExp = new RegExp(/^[0-9]+(\.[0-9]*){0,1}$/g);
// Allow key codes for special events. Reflect :
// Backspace, tab, end, home
private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home'];
constructor(private el: ElementRef) {
}
@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
// Allow Backspace, tab, end, and home keys
if (this.specialKeys.indexOf(event.key) !== -1) {
return;
}
// Do not use event.keycode this is deprecated.
// See: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
let current: string = this.el.nativeElement.value;
// We need this because the current value on the DOM element
// is not yet updated with the value from this event
let next: string = current.concat(event.key);
if (next && !String(next).match(this.regex)) {
event.preventDefault();
}
}
}
<input OnlyNumber="true">
我的建议解决方案与@Manwal的相同: 添加
min="0" onkeypress="return isNumberKey(event)"
将其添加到HTML输入框中
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
return !(charCode > 31 && (charCode < 48 || charCode > 57));
}
仅供参考:使用jQuery,您可以通过以下代码在focusout时覆盖负值:
$(document).ready(function(){
$("body").delegate('#myInputNumber', 'focusout', function(){
if($(this).val() < 0){
$(this).val('0');
}
});
});
这并不替代服务器端验证!
只需使用min="0"
<v-text-field
v-model="abc"
class="ml-1 rounded-0"
outlined
dense
label="Number"
type="number"
min="0">
</v-text-field>