2009-12-17 99 views
0

有沒有辦法檢測從textarea或多行文本框中插入的字符串中是否有連續的回車符?在字符串末尾檢測兩個連續的回車符

這裏是場景: 在文本區域,用戶輸入ABCD「Enter」EFGHI「Enter」JKLMNOP「Enter」「Enter」。在這之後,我需要強制按鈕的單擊事件 - 而不是form.submit。

下面是Default.aspx頁面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<%@ Register Src="~/UserControls/Search.ascx" TagName="Search" TagPrefix="ucSearch" %> 
<html xmlns="w3.org/1999/xhtml">; 
<head runat="server"> 
<title></title> </head> 
<body> 
<form id="form1" runat="server"> 
<asp:scriptmanager runat="server"></asp:scriptmanager> 
<div> 
<ucSearch:Search id="search1" runat="server" /> 
</div> 
</form> 
</body> 
</html> 

這是Search.ascx頁:

<script language="javascript"> 
var inputString function doit(){inputString = document.getElementById("search1$txtSearchText").value; 
if (inputString.match(/(\n\n|\r\r|\r\n\r\n)$/)) { 
    document.getElementById("search1_btnFindAssets").click(); 
} 

</script> 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Search.ascx.cs" Inherits="UserControls_Search"%> 
<asp:TextBox ID="txtSearchText" TextMode="MultiLine"onKeyPress="doit();" runat="server"> 
</asp:TextBox> 
<br> 
<asp:ButtonID="btnFindAssets"runat="server"Text="Find"onclick="btnFindAssets_Click"> 
+1

你是指在字符串的末尾或字符串中的任何位置?您的標題和問題主體不匹配。 – 2009-12-17 22:49:34

回答

1

是的,你可以用正則表達式做到這一點:

if (s.match(/\r\r/)) { ... } 

\r字符與回車符匹配。也許你的意思是換行(\n)?您可能還想要處理不同類型的新行'\ r','\ r \ n'或'\ n'。你可以這樣做是這樣的:

if (s.match(/\n\n|\r\r|\r\n\r\n/)) { ... } 

如果你只是想匹配的字符串的結尾,使用正則表達式符號$

if (s.match(/\r\r$/)) { ... } 

或:

if (s.match(/(\n\n|\r\r|\r\n\r\n)$/)) { ... } 
+0

感謝您的信息。這在您第三次點擊「Enter」時有效。例如,我輸入「FirstSearchWord」,點擊「Enter」(光標前進到下一行),「SecondSearchWord」,點擊「Enter」(光標前進到下一行),點擊「回車」除非我必須在頁面回發之前再次點擊「Enter」。任何想法爲什麼以及如何消除額外的「回車」?謝謝 – Risho 2009-12-21 17:44:34

+0

所以你想在用戶在文本框中輸入兩行後自動提交? – 2009-12-22 10:00:32

3
var isDoubled = yourString.indexOf("\n\n") != -1; 
1
if (/[\r\n]{2,}/.test(myString)) 
{ 
//TODO 
} 

查找兩個或更多個連續carrag e返回/新行在任何字符串中的任何位置。

+1

「這隻包含一個\ r \ n新行」失敗。 – 2009-12-18 07:27:23

+0

千萬不要撿起第二個回車。 – Risho 2009-12-21 17:36:06