Validating textboxes in C#
-
Hi This code here below allows only numbers and one decimal point to be typed on a textbox. It does not prevent you from: 1. Saving nulls in the database 2. From pasting any other character(words, asterisks,numbers etc) to the textbox. What do i have to add to the code to prevent the two problems from occurring?? Here is my code:
private void txtBalance\_TextChanged(object sender, EventArgs e) { txtBalance.KeyPress += new KeyPressEventHandler(numbercheck\_KeyPress); } private void numbercheck\_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } //only allow one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } }
Please help me figure out Thanks
-
Hi This code here below allows only numbers and one decimal point to be typed on a textbox. It does not prevent you from: 1. Saving nulls in the database 2. From pasting any other character(words, asterisks,numbers etc) to the textbox. What do i have to add to the code to prevent the two problems from occurring?? Here is my code:
private void txtBalance\_TextChanged(object sender, EventArgs e) { txtBalance.KeyPress += new KeyPressEventHandler(numbercheck\_KeyPress); } private void numbercheck\_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } //only allow one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } }
Please help me figure out Thanks
First, you don't want to add the KeyPressEventHandler in the in the TextChanged event. Every time this event is fired it will add a new event handler, you only need it added once in your form load or in it method, or declaratively in the markup. Before saving to the database you should validate the input, KeyPressEventHandler has nothing to do with this.
Failure is not an option; it's the default selection.
-
First, you don't want to add the KeyPressEventHandler in the in the TextChanged event. Every time this event is fired it will add a new event handler, you only need it added once in your form load or in it method, or declaratively in the markup. Before saving to the database you should validate the input, KeyPressEventHandler has nothing to do with this.
Failure is not an option; it's the default selection.
-
Hi This code here below allows only numbers and one decimal point to be typed on a textbox. It does not prevent you from: 1. Saving nulls in the database 2. From pasting any other character(words, asterisks,numbers etc) to the textbox. What do i have to add to the code to prevent the two problems from occurring?? Here is my code:
private void txtBalance\_TextChanged(object sender, EventArgs e) { txtBalance.KeyPress += new KeyPressEventHandler(numbercheck\_KeyPress); } private void numbercheck\_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } //only allow one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } }
Please help me figure out Thanks
Rather than trying to prevent "wrong" input, you should check the data when the user has finished input. Even when you prevent copy/paste of invalid data, some other technology might be used to get a "wrong" input into the box, e.g. a WM_SETTEXT message (think of assistive technologies). The "Validate" event of the textbox is a good place to do so.
-
You need an example of string.IsNullOrEmpty? Or how to validate a string :confused: if so then you need more help than can be given here.
Failure is not an option; it's the default selection.
-
if(string.IsNullOrEmpty(TextBox1.Text)){}
-
if(string.IsNullOrEmpty(TextBox1.Text)){}
If u only want to validate decimal, then you can try following code at textbox change event:
private void textBox1\_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Trim() != "") { decimal j; if (!decimal.TryParse(textBox1.Text, out j)) { MessageBox.Show("Not allowed!!!"); textBox1.Text = ""; } } }
Here, replace "textBox1" with your textbox ID.
-
First, you don't want to add the KeyPressEventHandler in the in the TextChanged event. Every time this event is fired it will add a new event handler, you only need it added once in your form load or in it method, or declaratively in the markup. Before saving to the database you should validate the input, KeyPressEventHandler has nothing to do with this.
Failure is not an option; it's the default selection.
If u only want to validate decimal, then you can try following code at textbox change event:
void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Trim() != "")
{
decimal j;
if (!decimal.TryParse(textBox1.Text, out j))
{
MessageBox.Show("Not allowed!!!");
textBox1.Text = "";
}
}
}private Here, replace "textBox1" with your textbox ID.
-
Hi This code here below allows only numbers and one decimal point to be typed on a textbox. It does not prevent you from: 1. Saving nulls in the database 2. From pasting any other character(words, asterisks,numbers etc) to the textbox. What do i have to add to the code to prevent the two problems from occurring?? Here is my code:
private void txtBalance\_TextChanged(object sender, EventArgs e) { txtBalance.KeyPress += new KeyPressEventHandler(numbercheck\_KeyPress); } private void numbercheck\_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } //only allow one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } }
Please help me figure out Thanks
The database is irrelevant to this question. Your database schema should prevent nulls, and you want a control that gives you a double. A good numeric text box should:
- Hook TextChanged to make sure that the value can never be invalid, e.g. if you try to paste into it, set the text programatically (e.g. through data binding) or any other way of putting text in there. Note that 'valid content' isn't necessarily a valid number, because you need to allow intermediate input that someone will type while entering a number. For example '', '-', '3.', '6E' (if supporting exponential notation) and the like should be allowed. You can find or write a regex for this part.
- Hook the keyboard input events (KeyPress should be enough) to reject any characters that can never be allowed, e.g. anything but numbers, or a minus sign in an empty field, or a dot. This prevents the TextChanged handler from firing, wiping the invalid character but resetting the caret.
- Have a numeric Value property that you can read and write in code.
-
Hi This code here below allows only numbers and one decimal point to be typed on a textbox. It does not prevent you from: 1. Saving nulls in the database 2. From pasting any other character(words, asterisks,numbers etc) to the textbox. What do i have to add to the code to prevent the two problems from occurring?? Here is my code:
private void txtBalance\_TextChanged(object sender, EventArgs e) { txtBalance.KeyPress += new KeyPressEventHandler(numbercheck\_KeyPress); } private void numbercheck\_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } //only allow one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } }
Please help me figure out Thanks
A NumericUpDown already has everything you need.
-
Hi This code here below allows only numbers and one decimal point to be typed on a textbox. It does not prevent you from: 1. Saving nulls in the database 2. From pasting any other character(words, asterisks,numbers etc) to the textbox. What do i have to add to the code to prevent the two problems from occurring?? Here is my code:
private void txtBalance\_TextChanged(object sender, EventArgs e) { txtBalance.KeyPress += new KeyPressEventHandler(numbercheck\_KeyPress); } private void numbercheck\_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } //only allow one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } }
Please help me figure out Thanks
My Simple Numeric TextBox[^] may be a good place to start. It doesn't do everything you require but could with a little effort.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
My Simple Numeric TextBox[^] may be a good place to start. It doesn't do everything you require but could with a little effort.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
And neither is the article/source - all C#!
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)