本文介绍了如何使用比较验证器的Windows窗体应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 (我想添加验证器以输入余额....如果我们输入小于1000的值,它应该显示一些消息) 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 =输入有效手机号码; } Regex expr1 =新正则表达式( @^ [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 =输入有效电子邮件; if(textBox6.TextLength< 4 ) label12.Text =PIN应该只有4位数字; if(textBox6.TextLength> 4) label12.Text =PIN应该只有4位数字; 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(); } } (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(); } }推荐答案 ); if(expr.IsMatch(textBox4.Text)== false) { label14.Text =输入有效的手机号码; } Regex expr1 =新的正则表达式(@^ [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 (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) l abel13.Text =输入有效的电子邮件; if(textBox6.TextLength< 4) label12.Text =PIN应该只有4位数字; if(textBox6.TextLength> 4) label12。 Text =PIN应该只有4位数字; else { SqlConnection con = new SqlConnection(@data source = ABHINAV- PC\ABHI;集成安全性=真;初始目录= 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(); } } "); 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(); } } 所以:而不是使用 So: Instead of using if (textBox6.TextLength < 4) label12.Text="PIN should have only 4 digits";if (textBox6.TextLength > 4) label12.Text = "PIN should have only 4 digits"; 使用TryParse将数字转换为整数: Convert the number to an integer, using TryParse:int PIN;if (!int.TryParse(textBox6.Text, out PIN) || PIN < 1000 || PIN > 9999) { label12.Text = "Invalid PIN!"; }else {然后将整数传输到SQL。 但是......有几件事: 1)不要这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。 2)请不要在所有内容中使用VS默认名称:这样可以更加难以找出正在发生的事情,特别是当你来cak更改它时几个月的时间你今天可能还记得textBox6是PIN码,但是你不会在六周内完成!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! 这篇关于如何使用比较验证器的Windows窗体应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 14:11