views:

393

answers:

3

I want to validate maxlegnth of 5 characters in each row of the multiline textbox

Help me

A: 

using split function(both in C# and Javascript) and then check length it.

var temp = [TextFromTextBox].split('\n');
foreach(var s in temp)
{
   if(!ValidateFunction(s))
   {
      // code for show exception
   }
}
Soul_Master
Thank you.but iam not getting properly.please send me detailed code in javascript or C#.
+3  A: 

Here's an example: A TextArea and span to show the validation results.

<textarea cols="30" rows="10" onblur="validateRows(this)"></textarea><br/>
<span id="validationResults" style="color:red"></span>

Here's the JavaScript code to validate each row:

function validateRows(e){
   var content = e.value.split("\n");
   for(var line in content){
     var charLength = content[line].length - 1;
     var lineNumber = parseInt(line) + 1;
     if(charLength > 5){
       document.getElementById("validationResults").innerHTML += "* line " 
                         + lineNumber + " has " + charLength 
          + " characters" + "<br/>";
     }
   }
}
Jose Basilio
@rohith - I updated the code to include a "validation results" span instead of an alert. Give it a try.
Jose Basilio
A: 

This is a C# version. Can be used either in Web applications for server side validation or Windows applications. (In Web applications for client side validation, Jose Basilio's code is appropriate)

    public static bool HasMax5CharsPerLine(TextBox target)
    {
        foreach (string Line in target.Text.Split(new char[] {'\n'}))
            if (Line.Length > 5)
                return false;
        return true;
    }
M. Jahedbozorgan
thank you buttarget.Lines this property is not working
Sorry, the Lines property is just for System.Windows.Forms.TextBox and is not available for System.Web.UI.WebControls.TextBox.[I have edited my post to work for both.]
M. Jahedbozorgan
Acutally iam looking for regular expression in which it shodn't allow more dan 5 characters per row
The above code is not working for more than 2 rows