Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(I want to add validator for entering balance....if we enter value less than 1000 it should show some message)
private void button1_Click(object sender, EventArgs e)
{
Regex expr = new Regex(@"^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}9[0-9](\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$");
if (expr.IsMatch(textBox4.Text) == false)
{
label14.Text = "Enter Valid Mobile No";
}
Regex expr1 = new Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (expr1.IsMatch(textBox3.Text) == false)
label13.Text = "Enter Valid Email";
if (textBox6.TextLength < 4)
label12.Text="PIN should have only 4 digits";
if (textBox6.TextLength > 4)
label12.Text = "PIN should have only 4 digits";
else
{
SqlConnection con = new SqlConnection(@"data source=ABHINAV-PC\ABHI;integrated security=true;initial catalog=ATM;");
con.Open();
SqlCommand cmd = new SqlCommand("insert into customer_details(name,address,email_id,contact_number,card_number,pin,city,account_type,balance) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + comboBox1.Text + "','" + textBox9.Text + "')", con);
cmd.ExecuteNonQuery();
comboBox1.Text = " ";
textBox9.Text = " ";
textBox8.Text = " ";
textBox7.Text = " ";
textBox6.Text = " ";
textBox5.Text = " ";
textBox4.Text = " ";
textBox3.Text = " ";
textBox2.Text = " ";
textBox1.Text = " ";
textBox1.Focus();
con.Close();
}
}
Posted

1 solution

So: Instead of using
C#
if (textBox6.TextLength < 4)
   label12.Text="PIN should have only 4 digits";
if (textBox6.TextLength > 4)
   label12.Text = "PIN should have only 4 digits";

Convert the number to an integer, using TryParse:
C#
int PIN;
if (!int.TryParse(textBox6.Text, out PIN) || PIN < 1000 || PIN > 9999)
   {
   label12.Text = "Invalid PIN!";
   }
else
   {
Then transfer the integer to SQL.
But... a couple of things:
1) Don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) Please don't use VS default names for everything: it makes it a lot harder to work out what is going on, particularly when you come cak to change it in a months time. You might remember today that textBox6 is the PIN number, but you won't in six weeks time!
 
Share this answer
 
Comments
Abhinav Chaudhary 15-Mar-14 6:14am    
Regex expr2 = new Regex(@"^[1000-9999999]\d+$");
if (expr2.IsMatch(textBox9.Text)==false)
{
label16.Text = "Enter at least 1000Rs";
}
I have done this with my code...but i think this is wrong format...
OriginalGriff 15-Mar-14 6:18am    
It's - not to put too fine a point on it - complete nonsense as a Regex.
Do yourself a favour: Get a copy of Expresso
http://www.ultrapico.com/Expresso.htm
It's free, and it examines and generates Regular expressions. I use it a lot, and I wish I'd written it...
Abhinav Chaudhary 15-Mar-14 6:27am    
Okay Sir,Thank You!
Abhinav Chaudhary 15-Mar-14 6:17am    
And Thanks for the code of PIN
OriginalGriff 15-Mar-14 6:20am    
You're welcome - but I'd use the same to check your textBox9 value - whatever the heck that is!
Seriously: I wasn't kidding about default names, or SQL Injection.

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