clear field if only html tags exist in text field

Asked

Viewed 210 times

1

I have a text field that allows html codes. When the field is empty the tags are passed <p><br></p> below the screens, if I give several enter in the field below the cloths is inserted several tags <p>. How can I do a validation to check if the field is empty in this way?

Example: I typed several enter and saved the text, in the backend is shown the value of "<p><br></p><p><br></p><p><br></p> for the field, however it is empty, I wanted to do a validation so that if there are ONLY HTML tags it means that the field is empty and I cannot allow you to save.

I did the validation:

 if (string.IsNullOrEmpty(model.InformacaoObjetivo))
                    {
                        ErrorMessage = "Por favor, preencha o campo Texto.";
                    }

but is not entering if because although the field is empty there are tags that are passing to the field.

  • It’s not good practice what I’m about to say, but try: model.InformacaoObjetivo.Substring(model.InformacaoObjetivo.IndexOf('<'), model.InformacaoObjetivo.LastIndexOf('>') + 1).Trim(). This will cut all tags present in the string

  • 1

    but I can not cut the tags pq is allowed to use, example if I put there <p> and type a text, have to allow, but when you have only <p> without any text I can not allow ?

  • Change how things work "under the covers", ie, handle before inserting html tags. If the person only gave enter or spaces, instead of inserting <p>... do not enter anything

3 answers

0


Swap your < p>, < br>, and < /p> for empty and check the content.

string temp = model.InformacaoObjetivo;
temp = temp.replace("", "<p>");
temp = temp.replace("", "</p>");
temp = temp.replace("", "<br>");
if (string.IsNullOrEmpty(temp))
{
    ErrorMessage = "Por favor, preencha o campo Texto.";
}

If you passed the if, it’s because model.Informationthe object is not empty.

  • but when I use the <p> tag? will be removed and cannot, I have to remove only if there is no content in the field

  • Your code won’t produce what you want to do. temp will not have its value changed because Replace (and not replace) returns a new String with the text changed, and does not change its invocator.

  • 2

    you will remove the <p> tags just to test whether the string is empty. The temp variable is only for the test, you continue to work with the model.Information.

  • I can not do so because you are always ordering replace <br> by empty, if I have a text with new paragraph for example in HTML will not catch because you are asking forever remove

  • If you have a text: "any text<br>" and replace br with nothing, and check that the rest of the string is empty, you will know that it is not. Removing tags is only to check for text...

  • Got it, I’ll leave it anyway, thanks

Show 1 more comment

0

I suggest you use Regex to check if there are characters inside the tags.

Regex regex = new Regex("[a-z]");
var html = "<p></p>";



Match match = regex.Match(html);
if (regex.IsMatch(html))
{
  Console.WriteLine("Por favor, preencha o campo Texto.");
}
  • So it won’t work, I gave several enter in the field and then passed dfg, in this case it has to allow because there are "texts" in the field, but in this condition the bar system because there are <p tags>

0

In my opinion it would be easier (not only easy, but smart) to treat this inconsistent html instead of dealing with it when it exists. But.. How about using a library? Try HtmlAgilityPack

public void Main()
{
    var caso1 =  @"<p><br></p>";
    var caso2 =  @"
        <p><br></p>
        <p><br></p>
        <p><br></p>";
    var caso3 =  @"<p><br><p>hola</p></p>";

    Console.WriteLine(checaHtml(caso1)); //1 -> ok
    Console.WriteLine(checaHtml(caso2)); //3 -> tratar, seilá reescrever o valor do caso 1
    Console.WriteLine(checaHtml(caso3)); //1 -> ok
}

public int checaHtml(string html) {
    var htmlDoc = new HtmlDocument();
    htmlDoc.LoadHtml(html);     
    var elem = htmlDoc.DocumentNode.SelectNodes("p/br");
    if (elem != null){
        return elem.Count;
    }
    else {
        return 0;
    }
}

Example: https://dotnetfiddle.net/yWzWWK

Browser other questions tagged

You are not signed in. Login or sign up in order to post.