Allow Numbers After Decimal And Before In Jquery
Example: In this function it will takes 10 digits before decimal and after decimal it will takes 8 digits.
Lets see how it is possible in Javascript, if you want to change it in Jquery also
Please follow these steps:
1. Create a textbox with the name of txtDecimal and add a event with the name of onkeypress to that textbox and give maxlength of that textbox. See below.
<asp:TextBox ID="txtDecimal" runat="server" MaxLength="20" onkeypress="return CheckNumber(event,this.value)"></asp:TextBox>
5 digits before decimal and after
10 digits before decimal and after 8 digits
8 digits before decimal and after 6 digits with accepting minus (-) symbol.
8 digits before decimal and after 8 digits with accepting minus (-) symbol.
10 digits before decimal and after 8 digits with accepting minus (-) symbol.
Lets see how it is possible in Javascript, if you want to change it in Jquery also
Please follow these steps:
1. Create a textbox with the name of txtDecimal and add a event with the name of onkeypress to that textbox and give maxlength of that textbox. See below.
<asp:TextBox ID="txtDecimal" runat="server" MaxLength="20" onkeypress="return CheckNumber(event,this.value)"></asp:TextBox>
2. Create a Javascript function with the name of CheckNumber in aspx page.
<script type="text/javascript">
function CheckNumber(key, value) {
var dot= /[.]/g;
var whitespace = /^\S+$/;
var Minus = /[-]/g;
var keyCode = key.keyCode == 0 ? key.charCode : key.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 45) || (keyCode == 46));
if (ret == true) {
if (keyCode != 8) {
var Strlength = value.length;
var StrValue = value;
if (keyCode == 46) {
if (value.indexOf('.') > -1) {
return false;
}
}
else if (keyCode == 45) {
if (value.indexOf('-') > -1) {
return false;
}
}
else {
// Here it will take upto 10 digits before decimal and after decimal it will take 8 digits.
var str = [10, 8];
if (value.indexOf('.') > -1) {
if (str[0] != "") {
var length = value.substr(value.indexOf('.') + 1).length;
if (length >= str[1]) {
return false;
}
}
}
if (!StrValue.match(dot)) {
var strHypen = StrValue.replace("-", "");
var strHypenLength = strHypen.length;
if (str[0] != "") {
//Here I am taken maxlength of textbox is 20 for my requirement, whatever your requirement you have to change.
if (strHypenLength > 20 - str[1] - 3) {
if (Strlength > 20 - str[1] - 3) {
return false;
}
}
}
}
}
}
}
else {
return ret;
}
}
</script>
function CheckNumber(key, value) {
var dot= /[.]/g;
var whitespace = /^\S+$/;
var Minus = /[-]/g;
var keyCode = key.keyCode == 0 ? key.charCode : key.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 45) || (keyCode == 46));
if (ret == true) {
if (keyCode != 8) {
var Strlength = value.length;
var StrValue = value;
if (keyCode == 46) {
if (value.indexOf('.') > -1) {
return false;
}
}
else if (keyCode == 45) {
if (value.indexOf('-') > -1) {
return false;
}
}
else {
// Here it will take upto 10 digits before decimal and after decimal it will take 8 digits.
var str = [10, 8];
if (value.indexOf('.') > -1) {
if (str[0] != "") {
var length = value.substr(value.indexOf('.') + 1).length;
if (length >= str[1]) {
return false;
}
}
}
if (!StrValue.match(dot)) {
var strHypen = StrValue.replace("-", "");
var strHypenLength = strHypen.length;
if (str[0] != "") {
//Here I am taken maxlength of textbox is 20 for my requirement, whatever your requirement you have to change.
if (strHypenLength > 20 - str[1] - 3) {
if (Strlength > 20 - str[1] - 3) {
return false;
}
}
}
}
}
}
}
else {
return ret;
}
}
</script>
Result: Actually it will accept only numbers and one dot(.) and one (-) symbol.
Here it is output.
10 digits before decimal and after 8 digits
8 digits before decimal and after 6 digits with accepting minus (-) symbol.
8 digits before decimal and after 8 digits with accepting minus (-) symbol.
10 digits before decimal and after 8 digits with accepting minus (-) symbol.
Hope it will help you guys..!!!!!!!!!!
Comments
Post a Comment