I want to enter a decimal point in a text box. I want to restrict the user by not allowing more than 1 digits after the decimal point.And restrict after decimal point enter only the number 5. I have written the code for it in the Keypress event. Using keypress event i want the series like:
1,1.5,2,2.5,3,3.5,4,4.5,5.
only the number between 1 and 5. how can i do this?
Check carat position to allow character insertion before the decimal. correct issue pointed out by ddlab's comment and only allow one dot.. The code is working but i have an issue if i enter 10 its working, i am not able to do so.i want numbers between 1 and 5.
function checkDecimal[_this, EventKey] {
// var key = EventKey.which || EventKey.keyCode;
// if [[key = 48 && _this.value.length == 0] || key == 8 || key == 9 || key == 37 || key == 39] {
// return true;
// }
// else {
// return false;
//}
var charCode = [EventKey.which] ? EventKey.which : event.keyCode;
var number = _this.value.split['.'];
if [charCode != 46 && charCode > 31 && [charCode < 49 || charCode > 53]] {
return false;
}
//just one dot [thanks ddlab]
if [number.length > 1 && charCode == 46] {
return false;
}
//get the carat position
var caratPos = getSelectionStart[_this];
var dotPos = _this.value.indexOf["."];
if [caratPos > dotPos && dotPos > -1 && [number[1].length > 0]] {
return false;
}
return true;
}
function getSelectionStart[o] {
if [o.createTextRange] {
var r = document.selection.createRange[].duplicate[]
r.moveEnd['character', o.value.length]
if [r.text == ''] return o.value.length
return o.value.lastIndexOf[r.text]
} else return o.selectionStart
}
its not working properly,Can you tell me what could be the issue?
i want to enter one digit before and after dot.not allow characters only integers
Hi all,
on one textbox i only want to enter numbers and decimal points. what i need is there should be only numbers , only one decimal points , back space allowd. no other special chars,no alphabets.
Can someone have any example.
function isNumberKey[evt] { var charCode = [evt.which] ? evt.which : event.keyCode if [charCode > 31 && [charCode < 48 || charCode > 57]] return false; return true; }
i tried this on keypress on textbox but this allows only numbers not decimal poitns
Try this
function isNumberKey[evt, obj] { var charCode = [evt.which] ? evt.which : event.keyCode var value = obj.value; var dotcontains = value.indexOf["."] != -1; if [dotcontains] if [charCode == 46] return false; if [charCode == 46] return true; if [charCode > 31 && [charCode 57]] return false; return true; }
function checkDec[el]{ var ex = /^[0-9]+\.?[0-9]*$/; if[ex.test[el.value]==false]{ el.value = el.value.substring[0,el.value.length - 1]; } }
Updated 24-Feb-14 19:56pm
function validCheck[e] { var keyCode = [e.which] ? e.which : e.keyCode; if [[keyCode >= 48 && keyCode 31 && [keycode < 48 || keycode > 57] && keycode != 46] {
alert[" You can enter only characters 0 to 9 "];
return false;
}
else return true;
}function isNumber[evt] {
evt = [evt] ? evt : window.event;var charCode = [evt.which] ? evt.which : evt.keyCode;
if [charCode > 31 && [charCode < 46 || charCode > 57 ] ] {
return false;
}
return true;
}Updated 25-Feb-19 18:19pm
function isNumberDecimal[evt, element] {
debugger;
var charCode = [evt.which] ? evt.which : event.keyCodeif [
[charCode != 46 || $[element].val[].indexOf['.'] != -1] &&
[charCode < 48 || charCode > 57]]
return false;return true;
}This content, along with any associated source code and files, is licensed under The Code Project Open License [CPOL]