I have a textbox that I want it to allow typing numbers from 0.00 to 24.00 only.
<asp:TextBox ID = "txtHours" runat = "Server"
onkeydown="limit(this,event);"
onkeypress="return validateFloatKeyPress(this,event);"
MaxLength="5" Width = "40" text = '<%#showData(Container.DataItem, "Hours")%>'/>
I tried using Javascript in onkeypress and onkeydown
It's still allowing typing numbers like 25,26,25.01... etc
I want it not to even allow typing 5,6,7...etc after 2 is typed.
function validateFloatKeyPress(el, evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var number = el.value.split('.');
var numberbfr = el.value.split('.')[0];
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
if (number.length > 1 && charCode == 46) {
return false;
}
if (numberbfr.length > 1) {
return false;
}
var caratPos = getSelectionStart(el);
var dotPos = el.value.indexOf(".");
if (caratPos > dotPos && dotPos > -1 && (number[1].length > 1)) {
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
}
function limit(el, evt) {
if (parseInt(el.value.charAt(0)) > 2 || (parseInt(el.value.charAt(0)) = 2 && parseInt(el.value.charAt(1)) > 4)) {
return false;
}
}
Try this.Hope this solution will help you.Copy and paste below code.'stack' is the id of the textbox.
function checkOnlyZeroTo24(evt, el) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (el.value == '' && !(charCode > 31 && (charCode < 48 || charCode > 57))) {
return true;
}
else if (el.value != '') {
if (el.value.charAt(0) < 3 && charCode == 46 && el.value.length == 1 && el.value.indexOf('.') == -1) {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
else if (el.value.charAt(0) < 3 && !(charCode > 31 && (charCode < 48 || charCode > 57)) && el.value.length == 1) {
document.getElementById('stack').setAttribute('maxlength', 5);
if (el.value.charAt(0) == 2 && (charCode < 48 || charCode > 52))
return false;
else
return true;
}
else if (el.value <= 24 && el.value >= 10 && charCode == 46 && el.value.indexOf('.') == -1) {
document.getElementById('stack').setAttribute('maxlength', 5);
return true;
}
else if (el.value.substr(0, el.value.indexOf('.')) <= 24 && el.value.substr(0, el.value.indexOf('.')) >= 10 && el.value.charAt(2) == '.' && !(charCode > 31 && (charCode < 48 || charCode > 57))) {
if (el.value.substr(0, el.value.indexOf('.')) == 24 && !(charCode == 48))
return false;
else
return true;
}
else if (el.value.charAt(0) < 3 && !(charCode > 31 && (charCode < 48 || charCode > 57)) && el.value.charAt(1) == '.') {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
else if ((el.value.charAt(0) >= 3 && charCode == 46 && el.value.indexOf('.') == -1) || (el.value.charAt(1) == '.' && !(charCode > 31 && (charCode < 48 || charCode > 57)))) {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
}
return false;
}
Asp:TextBox
<asp:TextBox ID = "stack" runat = "server" onkeypress="return checkOnlyZeroTo24(event,this);"> </asp:TextBox>
Related
I'm trying to make the input takes only the value 99.999. I don't want to use MaxLength because it would not calculate the length of the decimal digits. I don't want to use any other functions that erase when it doesn't match a specific regex. I want it to stop it in the input.
function IsCurrencyNoMinus1 (e, thisobj, min, max) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (keyCode == 46) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val();
if (ret && (keyCode == 45) && ((thisobj.selectionStart != 0) || (inStr.indexOf('-') != -1)))
ret = false;
if (ret && (keyCode == 46) && (inStr != '' && inStr.indexOf('.') != -1) && !(Math.abs(thisobj.selectionStart - thisobj.selectionEnd) == inStr.length)) {
ret = false;
}
var dotPos = (inStr.indexOf('.') != -1) ? inStr.indexOf('.') : inStr.length;
inStr = inStr.replace(/\,/g, '');
var parts = inStr.split('.');
var maxParts = max.toString().split('.');
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57))) {
if ((parts[0].length >= maxParts[0].length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart <= dotPos)) {
ret = false;
}
if (ret && (parts[1] != undefined && parts[1].length >= 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart > dotPos) && (thisobj.selectionStart <= dotPos + 3))
ret = false;
var firstPos = thisobj.selectionStart < thisobj.selectionEnd ? thisobj.selectionStart : thisobj.selectionEnd;
if (ret && (parts[0].length >= maxParts[0].length) && (parts[1] != undefined && parts[1].length >= 1)
&& ((dotPos - firstPos == 0 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4)
|| (dotPos - firstPos == 1 && (Math.abs(thisobj.selectionStart - thisobj.selectionEnd) >= 2 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4))))
ret = false;
}
if (Number(inStr) > max) {
thisobj.value = '';
ret = true;
}
if (Number(inStr) < min) {
thisobj.value = '';
ret = true;
}
// var re = new RegExp(/^\(?-?[0-9]{0,12}(\.[0-9]{0,2})?\)?$/)
// if (!re.test(inStr)) {
// thisobj.value = ""
// }
return ret
}
I found the solution! Please check the code below in case someone needs it.
function Format3DigitDecimal(e, thisobj, min, max)
{
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val()
inStr = inStr.replace(/\,/g, '')
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length >= max.toString().length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length == 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
return ret
}
I am using Struts2 <s:textfield /> tag and the below code calculates and checks / validates the value entered in the text field on the fly with JS.
It also updates the total entered in textfield list, these are handled by onblur and onkeydown events as below.
<s:textfield id = "fieldid"
name = "xyz"
onblur = "javascript:calculateTotal()"
onkeydown = "javascript:checkDecimal(this); return checkDecimal(event);"
/>
By using this code SHIFT+HOME , SHIFT+Left and SHIFT+Right keys functionalities are not working.
Is there any possibilities that this is due to onblur and onkeydown events ?
function checkDecimal(evt) {
var charCode;
var version = msieversion();
if(version==8){
charCode = (window.event) ? evt.keyCode : evt.which;
charCode = (window.event) ? evt.keyCode : evt.which;
}else{
charCode = (evt.which) ? evt.which : evt.keyCode;
}
if (!evt.shiftKey) {
if ((charCode >= 48 && charCode <= 57) ||
(charCode == 8) ||
(charCode == 46) ||
(charCode == 190) ||
(charCode == 35 ) ||
(charCode == 36) ||
(charCode == 9) ||
(charCode == 37) ||
(charCode == 39) ||
(charCode >= 96 && charCode <= 105) ||
(charCode == 110))
{
return true;
} else {
return false;
}
} else {
if (charCode == 9) {
return true;
} else {
return false;
}
}
}
You need to use the keypress event instead of the keydown one.
The keypress will intercept the resultant character,
while the keydown will interpret the SHIFT as a button itself.
Then try using
<s:textfield id = "fieldid"
name = "xyz"
onblur = "javascript:calculateTotal()"
onkeypress = "javascript:checkDecimal(this); return checkDecimal(event);"
/>
Code will look like this...
if (!evt.shiftKey) {
if ((charCode >= 48 && charCode <= 57) ||
(charCode == 8) ||
(charCode == 46) ||
(charCode == 190) ||
(charCode == 35 ) ||
(charCode == 36) ||
(charCode == 9) ||
(charCode == 37) ||
(charCode == 39) ||
(charCode >= 96 && charCode <= 105) ||
(charCode == 110))
{
return true;
} else {
return false;
}
} else if(evt.shiftKey) {
if ((charCode == 35 ) || (charCode == 39) || (charCode == 37) || (charCode == 36)) {
return true;
}
}
I assume you need to validate and allow only the decimal values. In that case this snippet will do the job.
<input id = "fieldid"
name = "xyz"
onblur = "javascript:calculateTotal()" onkeypress="return checkDecimal(event);" />
<script>
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function checkDecimal(evt) {
var charCode = (window.event) ? evt.keyCode : evt.which;
var currentVal = String.fromCharCode(charCode);
var val = document.getElementById(evt.currentTarget.id).value + '' + currentVal;
return (evt.shiftKey || isNumeric(val) || isNumeric(currentVal) || String.fromCharCode(currentVal) == '.');
}
</script>
Please feel free to correct me if I was not read the question correctly.
I Have One text box Which should accept decimal like 12345.678 That text box should accept . before 5 decimals and after 3 decimals how to validate for this type
You can use regular expressions to match values from textarea. Required regex would be:
/^\d{5}\.{1}\d{3}$/
http://www.regexr.com/3a8rn
Is this what you are looking for?
Fiddle: Demo
HTML
<input type="text" onkeypress = "return onlyDecimal(event,this)" onblur="validateValue(this)"/>
JS
function onlyDecimal(event, elem) {
var charCode = (event.which) ? event.which : event.keyCode;
console.log(elem.value.indexOf("."));
if(elem.value.length>=9)
{
validateValue(elem);
return false;
}
if (elem.value.indexOf(".") >= 0 && elem.value.split(".").length - 1 >= 0 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
if ((charCode == 190 || charCode == 110 || charCode == 46) && elem.value.length>=5) //for '.'
return true;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
}
function validateValue(elem){
if(elem.value.length<=9 && elem.value.indexOf(".") < 0)
{
alert("invalid value");
}
else if(elem.value.length<=9 && elem.value.indexOf(".") !=5)
{
alert("invalid value");
}
else if(elem.value.length>0 && elem.value.indexOf(".") >0)
{
var temp=elem.value.split(".");
if(temp[0].length<5 ||temp[1].length<3)
alert("invalid value");
}
}
How to check textbox validation for two numbers and two decimal values in asp.net with javascript?
For Example whien i press the key in textbox it should allow me only xx.xx format, example : 12.25, 25.50,48.45 etc.
I got the answer.
<div>
<asp:TextBox ID="TextBox2" runat="server"
onkeypress="return isDecimalNumber(event,this);" MaxLength="5">
</asp:TextBox>
</div>
<script type="text/javascript" language="javascript">
var count = 0;
function isDecimalNumber(evt, c) {
count = count + 1;
var charCode = (evt.which) ? evt.which : event.keyCode;
var dot1 = c.value.indexOf('.');
var dot2 = c.value.lastIndexOf('.');
if (count > 2 && dot1 == -1) {
c.value = "";
count = 0;
}
if (dot1 > 2) {
c.value = "";
}
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else if (charCode == 46 && (dot1 == dot2) && dot1 != -1 && dot2 != -1)
return false;
return true;
}
</script>
Try this,
$('.TextBox2').keypress(function (event) {
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
var text = $(this).val();
if ((text.indexOf('.') != -1) && (text.substring(text.indexOf('.')).length > 2)) {
event.preventDefault();
}
});
http://jsfiddle.net/hibbard_eu/vY39r/
$("#amount").on("keyup", function(){
var valid = /^\d{0,2}(\.\d{0,2})?$/.test(this.value),
val = this.value;
if(!valid){
console.log("Invalid input!");
this.value = val.substring(0, val.length - 1);
}
});
Hi i am new Java platform. I want to allow only 18 digit before decimal(.) and after decimal i want to enter only two number. For that have used that code snippet but it doesn't work for me.
if(detectBrowser() == "Firefox")
{
var charCode = evt.which;
var char = getChar(evt);
}
else if(detectBrowser() == "Chrome")
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
}
else
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
}
var flagy=false;
var a=el.value.split(".") ;
var b = el.value.indexOf(".");
if(b >-1)
{
flagy = true;
}
var key = String.fromCharCode(evt.keyCode);
var newLimit = /^[0-9]+$/i;
/* (charCode == 37 && key != "%") || (charCode == 39 && key != "'") || (charCode == 35 && key != "#") (charCode == 36 && key != "$") ||*/
if (charCode == 0 || charCode == 8 || charCode == 9 || (charCode == 97 && key != "a" && char =="a +ctrl")|| (charCode == 46 && el.value.indexOf(".")<0)) /* // back space, tab, delete, enter */
{
return true;
}
else if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
/* else if(flag == true)
{
}
*/else if(flagy == false)
{
if(el.value.length >15) return false;
}
return true;
Any help is really appreciated. Thanks in advance to all.
Try this definitely it will work.Here you have to use instead of
$("idMrp") ==use your text box id.
And then full code is here
if(detectBrowser() == "Firefox")
{
var charCode = evt.which;
var char = getChar(evt);
}
else if(detectBrowser() == "Chrome")
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
}
else
{
var charCode = (evt.which) ? evt.which : evt.keyCode;
}
var a=el.value.split(".") ;
var b = el.value.indexOf(".");
if(b >-1)
{
flag = true;
}
var key = String.fromCharCode(evt.keyCode);
var newLimit = /^[0-9]+$/i;
/* (charCode == 37 && key != "%") || (charCode == 39 && key != "'") || (charCode == 35 && key != "#") (charCode == 36 && key != "$") ||*/
if (charCode == 0 || charCode == 8 || charCode == 9 || (charCode == 97 && key != "a" && char =="a +ctrl")|| (charCode == 190 && el.value.indexOf(".") < 0)) /* // back space, tab, delete, enter */
{
return true;
}
else if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
else if(flag == true)
{
vala=a[0];
valb=a[1];
if(a[0]===vala){
if(a[1].length>2){
$("#idMrp").val(a[0]+"."+a[1].substring(0, a[1].length-1));
}
}
else{
if(a[0].length>16){
}
}