我是JS新手。你能找到我的問題,並從而建議? Js客戶端驗證不被調用。我也有服務器端驗證。通過單擊該按鈕僅調用服務器端驗證。你們能幫忙嗎?JS函數不在窗體內調用
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Users.aspx.cs" Inherits="JSApp.Users" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JsApp</title>
<script type="text/javascript">
function ValidatingForm() {
var b = true;
if (document.getElementById("FnTextBox").valueOf == "") {
document.getElementById("Label4").innerText = "beshi Required";
b = false;
} else {
document.getElementById("Label4").innerText = "";
}
if (document.getElementById("LnTextBox").valueOf == "") {
document.getElementById("Label5").innerText = "koravabe required";
b = false;
} else {
document.getElementById("Label5").innerText = "";
}
if (document.getElementById("EmailTextBox").valueOf == "") {
document.getElementById("Label6").innerText = "ajeeb vabe required";
b = false;
} else {
document.getElementById("Label6").innerText = "";
}
return b;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="FN"></asp:Label>
<asp:TextBox ID="FnTextBox" runat="server"></asp:TextBox>
<asp:Label ID="Label4" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="LN"></asp:Label>
<asp:TextBox ID="LnTextBox" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="EmailTextBox" runat="server"></asp:TextBox>
<asp:Label ID="Label6" runat="server"></asp:Label>
<br />
<br />
<asp:Button ID="sendButton" runat="server" OnClick="sendButton_Click" OnClientClick="return ValidatingForm()" Text="Send" />
</div>
</form>
</body>
</html>
aspx.cs:服務器端
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace JSApp
{
public partial class Users : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void sendButton_Click(object sender, EventArgs e)
{
if (ValidateForm())
{
SaveData();
}
}
private void SaveData()
{
string cs = ConfigurationManager.ConnectionStrings["SampleConnection"].ConnectionString;
SqlConnection connection=new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("spInsertUsers",connection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter fnParameter=new SqlParameter("@fn",FnTextBox.Text);
SqlParameter lnParameter=new SqlParameter("@ln",LnTextBox.Text);
SqlParameter emailParameter=new SqlParameter("@email",EmailTextBox.Text);
cmd.Parameters.Add(fnParameter);
cmd.Parameters.Add(lnParameter);
cmd.Parameters.Add(emailParameter);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
private bool ValidateForm()
{
bool b = true;
if (string.IsNullOrEmpty(FnTextBox.Text))
{
b = false;
Label4.Text = "Required";
}
else
{
Label4.Text = string.Empty;
}
if (string.IsNullOrEmpty(LnTextBox.Text))
{
b = false;
Label5.Text = "Required";
}
else
{
Label5.Text = "";
}
if (string.IsNullOrEmpty(EmailTextBox.Text))
{
b = false;
Label6.Text = "Required";
}
else
{
Label6.Text = "";
}
return b;
}
}
}
嘗試提交組件,並使用onsubmit事件。它可能會更好。 –
完整性檢查1 - 查看呈現的html並查看您的元素id是否符合您的期望。 – EdSF