2015-05-03 79 views
1

我是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> 
&nbsp; 
     <asp:TextBox ID="FnTextBox" runat="server"></asp:TextBox> 
&nbsp;<asp:Label ID="Label4" runat="server"></asp:Label> 
     <br /> 
     <br /> 
     <asp:Label ID="Label2" runat="server" Text="LN"></asp:Label> 
&nbsp; 
     <asp:TextBox ID="LnTextBox" runat="server"></asp:TextBox> 
&nbsp;<asp:Label ID="Label5" runat="server"></asp:Label> 
     <br /> 
     <br /> 
     <asp:Label ID="Label3" runat="server" Text="Email"></asp:Label> 
&nbsp; 
     <asp:TextBox ID="EmailTextBox" runat="server"></asp:TextBox> 
&nbsp;<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; 
     } 
    } 
} 
+0

嘗試提交組件,並使用onsubmit事件。它可能會更好。 –

+0

完整性檢查1 - 查看呈現的html並查看您的元素id是否符合您的期望。 – EdSF

回答

1

innerscript屬性沒有跨瀏覽器兼容性。使用textContent解決了這個問題。當我使用innerscript時,它在IE和Chrome中運行正常,但不能在Firefox中運行。這個問題我發佈的問題。

1

試試你按一下按鈕從改變:

<asp:Button ID="sendButton" runat="server" OnClick="sendButton_Click" OnClientClick="return ValidatingForm()" Text="Send" /> 

到:

<asp:Button ID="sendButton" runat="server" OnClick="sendButton_Click" OnClientClick="return ValidatingForm" Text="Send" /> 

看到這裏同樣的問題:

http://forums.asp.net/t/1069601.aspx?I+want+to+use+onsubmit+asp+net+wont+let+me+

1

這看起來或多或少是正確的。我會嘗試以下方法:

  1. 添加一個分號到您的OnClientClick處理器 - 即「的OnClientClick =」返回ValidatingForm();」
  2. 將一個JavaScript警告內部ValidatingForm(以確保它實際上叫)
  3. 檢查比較邏輯,可能的替代:的document.getElementById( 「FnTextBox」)的valueOf == 「」

關於嘗試如何:

document.getElementById("FnTextBox").value.length == 0