Hello,
Only allowed 10 Digit before and after 2 Decimal Point use following Javascript function.
Using this function you can
- Allowed maximum only 10 Digit before decimal
- Allowed 2 digit after decimal
- Only number and digit allowed
- Only one dot allowed
You need to call javascript function on Textbox onKeyPress Event
function validate(val) {
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
// for only numeric and dot allowed
if (!(charCode == 46 || (charCode >= 48 && charCode <= 57))) {
alert("Only numeric and dot allowed");
return false;
}
//for press char value
var curchar = String.fromCharCode(charCode);
//concate previous value with current press value
var mainstring = val + curchar;
//for only one dot allowed
if (mainstring.indexOf('.') > -1) {
if (mainstring.split('.').length > 2) {
alert("Only one dot allowed");
return false;
}
}
//for get beforeDecimal value string
var beforeDecimal = mainstring;
if (mainstring.indexOf('.') != -1) {
beforeDecimal = mainstring.substring(0, mainstring.indexOf('.') - 1);
}
//for get afterDecimal value string
var afterDecimal = '';
if (mainstring.indexOf('.') != -1) {
afterDecimal = mainstring.substring(mainstring.indexOf('.') + 1, mainstring.length);
}
//for check before decimal digit length
if (beforeDecimal.length > 10) {
alert("Maximum 10 digit allowed before decimal");
return false;
}
//for check after decimal digit length
if (afterDecimal.length > 2) {
alert("Only two decimal places allowed");
return false;
}
return true;
}
Applied this function into GridView Texbox
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtNumber" runat="server" onKeyPress="return validate(this.value);"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Hope this helpful!
Thanks