How can I break lines from an Errormessage of a Data Annotation?

Asked

Viewed 52 times

0

I have this property and the Data Annotations for it:

[Required(ErrorMessage = "Campo orbgiatório.")]
[RegularExpression("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$", ErrorMessage = "The password must contain: \n * Pelo menos 8 caracteres; \n *Pelo menos uma letra. \n *Pelo menos um dígito. ")]
public string Password { get; set; }

I want to skip the lines of the error message between each password rule, so I’m using the \n, but it’s not working. The \n just disappears and the message doesn’t break the lines.

  • Try it the Windows way\r\n. \n is Unix style.

  • Btw. Why do you post an English Question on a Portuguese site?

  • Hello. Pf put your question in Portuguese, is on Sopt. Ever tried to put Environment.NewLine instead of \n?

  • I confused the accounts, pardon :D

  • Neither r n nor Enviroment.Newline worked, unfortunately.

  • Where are you displaying this message? App Console, Windows App, or Web App?

  • It is a web page. But using <br> of the html in the message, as you replied, did not work. The message only displays the <br>. Which is very strange, because in your example, it worked;

Show 2 more comments

1 answer

0


If you are only displaying this validation in a web application you can use html tags for this:

[Required(ErrorMessage = "Campo orbgiatório.")]
[RegularExpression(
        "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$", 
        ErrorMessage = "<b>The password must contain:</b> <br> *Pelo menos 8 caracteres; <br> *Pelo menos uma letra. <br> *Pelo menos um dígito."
    )]
public string Password { get; set; }

inserir a descrição da imagem aqui

Browser other questions tagged

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