private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b') && (e.KeyChar != '.'))
{
e.Handled = true;
}
else
{
e.Handled = false;
}
if (Char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
{
TextBox tb = sender as TextBox;
int cursorPosLeft = tb.SelectionStart;
int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
string[] parts = result.Split('.');
if (parts.Length > 1)
{
if (parts[1].Length > 2 || parts.Length > 2)
{
e.Handled = true;
}
}
var arr = “0123456789.”;
var code;
var code1;
if (window.event)
code = e.keyCode;
else
code = e.which;
var cha = String.fromCharCode(code);
if (arr.indexOf(cha) == -1)
return false;
else if (cha == “.”)
if(txt.value.indexOf(“.”)>-1)
return false;
if(txt.value.indexOf(“.”)>-1)
{
code1=txt.value.indexOf(“.”);
if(txt.value.length == code1 + 3)
return false;
}