Check for empty lines and breaks in ASP.Net AjaxToolKit Editor

ilanocf
 
on Oct 25, 2021 10:49 PM
1306 Views

Hey guys,

I'm trying to check if a variable is empty by removing empty lines and breaks. For this I created the function below and a variable receiving text from an Editor (AjaxControlToolkit) but it doesn't work.

Function RemoveLinhasVazias(ByVal pTexto As String) As Boolean
    Dim pDelimiters As Char() = New Char() {vbCr, vbLf}
    Dim pLines As String() = pTexto.Split(pDelimiters, StringSplitOptions.RemoveEmptyEntries)
    Dim pResult As String = String.Join(Environment.NewLine, pLines)
    pResult = Replace(pResult, "<br/>", "")

    If Trim(pResult) = "" Then Return True Else Return False

End Function

Private Sub VerificaCampos()
    Dim I As Integer
    Dim pTexto As String
    pTexto = Trim(Server.HtmlEncode(Editor1.Content))
    I = 0

    If fn.RemoveLinhasVazias(pTexto) = True Then
        I = I + 1
        panEditor.CssClass = "alert alert-danger"
    Else
        If fn.RemoveLinhasVazias(pTexto) = False Then
            panEditor.CssClass = "form-panel"
        End If
    End If

    If I = 0 Then
        AssociaCampos()
    Else
        If I > 0 Then
            fn.Alerta("Por favor, preencha o(s) campo(s) em destaque.", Page)
        End If
    End If

End Sub

How can I resolve issue?

Grateful,

Ilano

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 26, 2021 05:48 AM

Hi ilancof,

Please refer below sample.

HTML

<asp:ScriptManager runat="server" />
<asp:TextBox ID="Editor1" runat="server" Width="300" Height="200" />
<ajaxToolkit:HtmlEditorExtender runat="server" TargetControlID="Editor1">
</ajaxToolkit:HtmlEditorExtender>
<br />
<asp:Button Text="Save" runat="server" OnClick="OnSave" />

Code

C#

public bool RemoveLinhasVazias(string pTexto)
{
    string[] pDelimiters = new string[] { "\n", "\r", "\n\r" };
    string[] pLines = pTexto.Split(pDelimiters, StringSplitOptions.RemoveEmptyEntries);
    string pResult = string.Join(Environment.NewLine, pLines);
    pResult = StripHTML(pResult);
    if (string.IsNullOrEmpty(pResult.Trim()))
    {
        return true;
    }
    else
    {
        return false;
    }
}
public static string StripHTML(string input)
{
    return System.Text.RegularExpressions.Regex.Replace(input, "<.*?>", String.Empty);
}

protected void OnSave(object sender, EventArgs e)
{
    string pTexto;
    pTexto = Editor1.Text.Trim();
    if (RemoveLinhasVazias(pTexto))
    {
        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Empty');", true);
    }
    else if (!RemoveLinhasVazias(pTexto))
    {
        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Not empty');", true);
    }
}

VB.Net

Public Function RemoveLinhasVazias(ByVal pTexto As String) As Boolean
        Dim pDelimiters As String() = New String() {vbLf, vbCr, vbLf & vbCr}
        Dim pLines As String() = pTexto.Split(pDelimiters, StringSplitOptions.RemoveEmptyEntries)
        Dim pResult As String = String.Join(Environment.NewLine, pLines)
        pResult = StripHTML(pResult)

        If String.IsNullOrEmpty(pResult.Trim()) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function StripHTML(ByVal input As String) As String
        Return System.Text.RegularExpressions.Regex.Replace(input, "<.*?>", String.Empty)
    End Function

    Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
        Dim pTexto As String
        pTexto = Editor1.Text.Trim()

        If RemoveLinhasVazias(pTexto) Then
            ClientScript.RegisterStartupScript(Me.GetType(), "", "alert('Empty');", True)
        ElseIf Not RemoveLinhasVazias(pTexto) Then
            ClientScript.RegisterStartupScript(Me.GetType(), "", "alert('Not empty');", True)
        End If
    End Sub

Screenshot