tags:

views:

155

answers:

3

Is there a way to detect if there are 2 consecutive carriage returns in a string ontained from a textarea or multiline text box?

Here is the scenario: In a text area, user enters ABCD "Enter" EFGHI "Enter" JKLMNOP "Enter" "Enter". After the this I need to force click event of the button - not form.submit.

Here is the default.aspx page:

<%@ 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>

This is the Search.ascx page:

<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  A: 

Yes, you can do this with a regular expression:

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

The \r character matches carriage return. Maybe you mean line feed (\n)? You might also want to handle different type of new line '\r', '\r\n', or '\n'. You can do this like this:

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

If you only want the match at the end of the string, use the regex symbol $:

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

or:

if (s.match(/(\n\n|\r\r|\r\n\r\n)$/)) { ... }
Mark Byers
Thanks for your post. This works when you hit "Enter" on 3rd time.For example, I enter "FirstSearchWord", hit "Enter" (cursor advances on nextline), "SecondSearchWord", hit "Enter" (cursor advances on nextline), hit "Enter" (this when it should post back), except I have to hit "Enter" again before page postback. Any ideas why and how to eliminate that additional "Enter"? Thanks
Risho
So you want to automatically submit after the user enters exactly two lines in the textbox?
Mark Byers
+3  A: 
var isDoubled = yourString.indexOf("\n\n") != -1;
AlbertEin
+2  A: 
if (/[\r\n]{2,}/.test(myString))
{
//TODO
}

Looks for two or more consecutive carrage return/new lines any where in a string.

Paul Creasey
This fails for "This contains only one\r\nnew line".
Mark Byers
Never picks up the 2nd carriage return.
Risho