2013-10-14 181 views
0

在我的項目中,我正在檢查用戶在註冊期間的電子郵件ID是否有效我正在使用Javascript.I在按鈕單擊和驗證期間獲取函數中的文本框的值,但該按鈕的點擊時它顯示微軟JScript運行時錯誤:所需的對象錯誤的任何一個都可以指出什麼錯就錯在我的代碼,Javascript Object Required

我的JavaScript函數,

function mailcheck() { 

    var txt = document.getElementById("emailid").value; 
    if (txt != "") { 
     var at = "@" 
     var dot = "." 
     var lat = str.indexOf(at) 
     var lstr = str.length 
     var ldot = str.indexOf(dot) 
     if (txt.indexOf(at) == -1) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     if (txt.indexOf(at) == -1 || txt.indexOf(at) == 0 || txt.indexOf(at) == ltxt) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     if (txt.indexOf(dot) == -1 || txt.indexOf(dot) == 0 || txt.indexOf(dot) == lstr) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     if (str.indexOf(at, (lat + 1)) != -1) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     if (txt.substring(lat - 1, lat) == dot || txt.substring(lat + 1, lat + 2) == dot) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     if (txt.indexOf(dot, (lat + 2)) == -1) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     if (txt.indexOf(" ") != -1) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 

     } 
    } 

    return true 
} 
+1

你有錯誤的行/行號? – geevee

+0

你可以發佈ASPX/HTML標記 –

+0

@GalV它沒有給出任何行號 – Rajesh

回答

2

檢查你的控制是"runat=server"如果是這樣,呈現的html控件名稱應該是li ke:#ctl00_ContentPlaceHolderMain_emailid。

嘗試改變這一點:

var txt = document.getElementById("emailid").value; 

這樣:

var txt = document.getElementById('<%=emailid.ClientID %>').value; 

的客戶端ID會給你的情況下,全名的控制是一個內容面板內。

在另一邊您正在訪問屬性obj.focus(),我看不到obj在範圍內聲明。

+0

對不起我的錯誤我正在集中Textbox,如果它是無效的 – Rajesh

+0

嗨我已經嘗試了像你說的像'Var txt = document.getelementId(「 ctl00_ContentPlaceHolderMain_emailid「).value',它的工作完美,謝謝指出錯誤 – Rajesh

+0

不用客氣@Rajesh。總是要記住,如果控制runat =服務器,它是在一個內容持有人面板中,它將呈現不同的名稱;) –

2

它似乎是指obj對象(如obj.focus()),但沒有聲明它。這可能會導致錯誤。

此外,如果文本元素具有runat="server" - 你必須提到它與document.getElementById('<%= emailid.ClientID %>')

+0

對不起我的錯誤我是關注文本框,如果它是無效的 – Rajesh

2

由於您使用C#,你應該使用.NET生成clientID的

document.getElementById('<%= emailid.ClientID %>')

0

你使用服務器端控件輸入電子郵件地址?因爲如果你的ID會在頁面渲染時改變,所以你必須使用

document.getElementById('<%= emailid.ClientID%>') 
0

你在混合你的變量名。 str和ltxt在哪裏定義?

試試這個:

function mailcheck() { 

    var txt = document.getElementById("emailid").value; 
    if (txt != "") { 
     var at = "@" 
     var dot = "." 
     var lat = txt.indexOf(at) 
     var ltxt = txt.length 
     var ldot = txt.indexOf(dot) 
     console.log('yeah'); 
     if (txt.indexOf(at) == -1) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 
     if (txt.indexOf(at) == -1 || txt.indexOf(at) == 0 || txt.indexOf(at) == ltxt) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     if (txt.indexOf(dot) == -1 || txt.indexOf(dot) == 0 || txt.indexOf(dot) == ltxt) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     if (txt.indexOf(at, (lat + 1)) != -1) { 
      alert("Invalid E-mail ID") 
      obj.focus() 
      return false 
     } 

     return true; 
    } 
    return false; 
} 
相關問題