I have one amount field in that i want to restrict user after decimal they can not use more than two values:
我有一个金额字段,我想在十进制后限制用户,他们不能使用两个以上的值:
- After decimal there should be only two values that are option.
- User can not type more than one decimal in the field.
在十进制之后,应该只有两个值是选项。
用户不能在字段中键入多个小数。
I am using following regix for that its not working can any one help.
我正在使用以下regix,因为它不起作用可以任何一个帮助。
function validate(evt) {
if (evt.keyCode == 8) { return true; }// for backspace problem in mozilla
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /[0-9 ]|\,\d{1,2}/;
if (!regex.test(key))
{
theEvent.returnValue = false;
if (theEvent.preventDefault) theEvent.preventDefault();
}
}
2 个解决方案
#1
0
This can actually be accomplished using HTML only by setting the STEP and MAX of your numeric field such as
这实际上只能通过设置数字字段的STEP和MAX来使用HTML来完成
<form>
<input type="number" name="myNumber" required=true step=".01">
<button>send</button>
</form>
please let me know if this is what your looking for but this will mean that any value entered must be numeric(thus only 1 decimal point) it must be less than or equal to 1 (you can set to .99 if you dont want to include 1) and it must be an increment of .01(so it can only have 2 decimal places.
如果这是你想要的,请告诉我,但这意味着输入的任何值必须是数字(因此只有1个小数点)它必须小于或等于1(如果你不想要,你可以设置为.99包括1)并且它必须是.01的增量(因此它只能有2个小数位。
Just re-read your question you can remove the max(i thought i read no more than 1.0)
只需重新阅读你的问题就可以删除max(我以为我读的不超过1.0)
#2
0
var previousValue;
function numberChanged(element){
if(validateMe(element)==false){
element.value=previousValue;
}else{
previousValue=element.value;
}
function validateMe(element){
wholeNumber=element.value.split(".")[0];
if(element.value.split(".").length==1){
if(wholeNumber<-1 || > 999999999){
return false;
}
}else if(element.value.split(".").length==2){
decimalValue=element.value.split(".")[1];
if(decimalValue>-1 && decimalValue<100){
if(wholeNumber<-1 || > 999999999){
return false;
}
}else{
return false;
}
}else{
return false;
}
}
<input type="text" id="myNumber" onChange="numberChanged(this)" />
#1
0
This can actually be accomplished using HTML only by setting the STEP and MAX of your numeric field such as
这实际上只能通过设置数字字段的STEP和MAX来使用HTML来完成
<form>
<input type="number" name="myNumber" required=true step=".01">
<button>send</button>
</form>
please let me know if this is what your looking for but this will mean that any value entered must be numeric(thus only 1 decimal point) it must be less than or equal to 1 (you can set to .99 if you dont want to include 1) and it must be an increment of .01(so it can only have 2 decimal places.
如果这是你想要的,请告诉我,但这意味着输入的任何值必须是数字(因此只有1个小数点)它必须小于或等于1(如果你不想要,你可以设置为.99包括1)并且它必须是.01的增量(因此它只能有2个小数位。
Just re-read your question you can remove the max(i thought i read no more than 1.0)
只需重新阅读你的问题就可以删除max(我以为我读的不超过1.0)
#2
0
var previousValue;
function numberChanged(element){
if(validateMe(element)==false){
element.value=previousValue;
}else{
previousValue=element.value;
}
function validateMe(element){
wholeNumber=element.value.split(".")[0];
if(element.value.split(".").length==1){
if(wholeNumber<-1 || > 999999999){
return false;
}
}else if(element.value.split(".").length==2){
decimalValue=element.value.split(".")[1];
if(decimalValue>-1 && decimalValue<100){
if(wholeNumber<-1 || > 999999999){
return false;
}
}else{
return false;
}
}else{
return false;
}
}
<input type="text" id="myNumber" onChange="numberChanged(this)" />