I have got a task to restrict textbox from alphabetic values. Only floating point values are possible. After the decimal we have to write out two digits. I did this but it doesn't work in Mozilla Firefox. How can I solve this problem?
My script is
$(function () {
$('#name').bind('paste', function () {
var self = this;
setTimeout(function () {
if (!/^[a-zA-Z]+$/.test($(self).val())) $(self).val('');
}, 0);
});
$('#salary').bind('paste', function () {
var self = this;
setTimeout(function () {
if (!/^\d*(\.\d{1,2})+$/.test($(self).val())) $(self).val('');
}, 0);
});
$('.decimal').keypress(function (e) {
var character = String.fromCharCode(e.keyCode)
var newValue = this.value + character;
if (isNaN(newValue) || hasDecimalPlace(newValue, 3)) {
e.preventDefault();
return false;
}
});
function hasDecimalPlace(value, x) {
var pointIndex = value.indexOf('.');
return pointIndex >= 0 && pointIndex < value.length - x;
}
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
return false;
else {
var len = document.getElementById("txtChar").value.length;
var index = document.getElementById("txtChar").value.indexOf('.');
if (index > 0 && charCode == 46) {
return false;
}
if (index >0 || index==0) {
var CharAfterdot = (len + 1) - index;
if (CharAfterdot > 3) {
return false;
}
}
}
return true;
}
});
html is
<b>Name</b>
<input type="text" id="name" /><br/>
<b>Salary</b>
<input type="text" id="txtChar" onkeypress="return isNumberKey(event)" name="txtChar" class="CsstxtChar" />
I have got a task to restrict textbox from alphabetic values. Only floating point values are possible. After the decimal we have to write out two digits. I did this but it doesn't work in Mozilla Firefox. How can I solve this problem?
My script is
$(function () {
$('#name').bind('paste', function () {
var self = this;
setTimeout(function () {
if (!/^[a-zA-Z]+$/.test($(self).val())) $(self).val('');
}, 0);
});
$('#salary').bind('paste', function () {
var self = this;
setTimeout(function () {
if (!/^\d*(\.\d{1,2})+$/.test($(self).val())) $(self).val('');
}, 0);
});
$('.decimal').keypress(function (e) {
var character = String.fromCharCode(e.keyCode)
var newValue = this.value + character;
if (isNaN(newValue) || hasDecimalPlace(newValue, 3)) {
e.preventDefault();
return false;
}
});
function hasDecimalPlace(value, x) {
var pointIndex = value.indexOf('.');
return pointIndex >= 0 && pointIndex < value.length - x;
}
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
return false;
else {
var len = document.getElementById("txtChar").value.length;
var index = document.getElementById("txtChar").value.indexOf('.');
if (index > 0 && charCode == 46) {
return false;
}
if (index >0 || index==0) {
var CharAfterdot = (len + 1) - index;
if (CharAfterdot > 3) {
return false;
}
}
}
return true;
}
});
html is
<b>Name</b>
<input type="text" id="name" /><br/>
<b>Salary</b>
<input type="text" id="txtChar" onkeypress="return isNumberKey(event)" name="txtChar" class="CsstxtChar" />
Share
Improve this question
edited Mar 29, 2013 at 10:31
gkiely
3,0071 gold badge25 silver badges37 bronze badges
asked Mar 29, 2013 at 8:13
Nithin ViswanathanNithin Viswanathan
3,2837 gold badges44 silver badges85 bronze badges
7
- 3 I believe this is the 4th time i'm seeing the same code in 2 days.. Can you actually figure anything out yourself? – Rob Commented Mar 29, 2013 at 8:14
- If you use the number type on the input, you have it all solved ! – adeneo Commented Mar 29, 2013 at 8:20
- check is any error in firebug console – Tamil Selvan C Commented Mar 29, 2013 at 8:24
- @Nithu can u post plete html code – PSR Commented Mar 29, 2013 at 8:30
- @Nithu can u create a link for this – PSR Commented Mar 29, 2013 at 8:33
2 Answers
Reset to default 4function isNumberKey(evt) {
var charCode = (evt.charCode) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
return false;
else {
var len = document.getElementById("txtChar").value.length;
var index = document.getElementById("txtChar").value.indexOf('.');
if (index > 0 && charCode == 46) {
return false;
}
if (index >0 || index==0) {
var CharAfterdot = (len + 1) - index;
if (CharAfterdot > 3) {
return false;
}
}
}
return true;
}
<input type="text" id="txtChar" onkeypress="return isNumberKey(event)" name="txtChar" class="CsstxtChar" />
You should change e.keyCode to e.charCode.
String.fromCharCode(e.charCode)