我正在尝试验证三个文本框,它们都只是文本字段。它们都使用 sqlserver 连接到数据库。
现在我想知道当表单仍然为空时如何阻止表单将空记录添加到我的数据库中。我有一些 Javascript 验证,它告诉用户该字段为空,但是在您单击消息框的确定后,页面刷新并添加了一条空记录。
如果有人可以帮助我编写一些代码,那就太好了。
我的 javascript 看起来像这样:
function myAddFunction() {
var a = document.forms["form1"]["txtFirstName"].value;
var b = document.forms["form1"]["txtLastName"].value;
var c = document.forms["form1"]["txtvercode"].value;
if (a == null || a == "") {
alert("Please Enter a value");
return false;
}
if (b == null || b == "") {
alert("Please Enter a value");
return false;
}
if (c == null || c == "") {
alert("Please Enter a value");
return false;
}
else {
alert("The Member has been added!");
return true;
}
}
我的 Default.aspx.cs 文件如下所示:
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(
@"Data Source=(LocalDB)\v11.0; AttachDbFilename='|DataDirectory|\employees.mdf';
Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO membership (FirstName, LastName, MembershipClass, VerificationCode) VALUES ('" + txtFirstName.Text + "', '" + txtLastName.Text + "', '" + dropmemberclass.Text + "', '" + txtvercode.Text + "')", conn);
cmd.ExecuteNonQuery();
txtFirstName.Text = "";
txtLastName.Text = "";
txtvercode.Text = "";
txtMemID.Text = "";
txtmemdelete.Text = "";
lblMemID.Text = "";
lblMemIDSearch.Text = "";
}