Click here to Skip to main content
16,015,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am unsure how to do it as i do not know how to start
It allows only two decimal places but i want to allow only a digit before decimal place also

What I have tried:

<asp:TextBox ID="txtrate" runat="server" >
function validateFloatKeyPress(el, evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var number = el.value.split('.');
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
//just one dot
if (number.length > 1 && charCode == 46) {
return false;
}
//get the carat position
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
}
Posted
Updated 6-Aug-16 3:53am
v2
Comments
Patrice T 6-Aug-16 15:01pm    
Don't Repost same question

1 solution

Don't do it in C# - do it at the client, in Javascript: JavaScript Form Validation[^]
 
Share this answer
 
Comments
Member 12652110 6-Aug-16 10:15am    
i want to validate that to limit to a digit before decimal point and two digits after decimal point

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900