I want to make an MVC View, which shows a string.
The string contains whitespaces (" ", "/t", "/n") and I want to show it in format defined in string.
My problem is, that if I return the string as ViewBag message, all of these whitespaces are lost. Is there any solution to resolve this problem?
A work around, replace \t with   and \n with <br/> and you could use,
#Html.Raw(ViewBag.Something)
Your issue is a web one where the browser ignores extraneous space. You need to convert all space characters to if you want to retain the number of spaces.
You have two options to achieve this.
use pre htnml tag
or
you have to hard code the space chars into non-breaking white spaces.
hope it will help you to some extent.
Related
When adding data with the block call.StorageValue, the string is saved in firebase with "/" before and after the string,
There does not seem to be any block to remove it, How can I do it?
It's a normal firebase function that allows to separate the values and read them as such.
Example :
on Firebase, "\"English-EN\"" is a single value sent from the app as English-EN
and "[\"863674037411046\",\"863674037411046\",\"863674037411046\",\"863674037411046\"]" is a list of numbers sent as 863674037411046.
Try to retrieve the value with a button and to a simple label and you should see that it's displayed without the extra characters.
Source:check my app "harpokrates". I've made it as a firebase DB management demo and it uses nothing else. All values are stored as you describe and are retrieved just fine, without extra symbols or any need to trim the text.
ps:However if you do have extra symbols at some point, check your use of lists and lists of lists that might generate excessive "\" if you made a mistake somewhere. You can also use the "trim" or "split text" blocks but that would be bad practice. Finding the code error is best.
This is likely an escape character that escapes the special character " (quotation marks). This is common practice to use \ as an escape character to indicate that the next character has special meaning, in this case it is not the start or end of a string but actually part of it.
As such you can't actually remove it (just the escape character) and should consider how you got a quotation mark in the string to begin with.
You should however be able to remove the entire quotation mark \"
Looking at an old ASP file, I am trying to figure out what would the best practice be in achieving the following:
I am receiving a string through a database connection.
This string is the displayed part of a select box.
I am required to output this string using Server.HTMLEncode(string).
However, since there are spaces in that string I get the output as .
What would the best practice be in converting back into actual whitespace?
To replace with whitespace in classic ASP I used the following code:
<%= Replace(Server.HTMLEncode(string)," "," ") %>
Where string is the variable that contains .
I have done a validations using MVC4 Data Annotations.
And i restricted the Special characters too.
But i want to allow Spaces between each words.
I tried this:
[Required(ErrorMessage = "Category Name should not be empty")]
[RegularExpression(#"^[a-zA-Z0-9]+$", ErrorMessage = "Special character should not be entered")]
public virtual string Name { get; set; }
Right now system is not allowing me to Insert. Because i have entered a space. Its considering the space as special char.
How do i allow space? the same time the above validation should work.
Myself found the answer.
Instead of #"^[a-zA-Z0-9]+$" we need to add like this #"^[a-zA-Z0-9'' ']+$".
''Single Space' in the Expression
'' '
For more information I'd suggesting reading - Using Data Annotations to Customize Data Classes (MSDN)
I just came across your answer and just wanted to mention that the single quotes are not needed. You just need the space inside the brackets.
#"^[a-zA-Z0-9 ]+$"
we need to add like this
Change symbol "+" to "*" (after close square brackets)
Correct answer is :
#"^[a-zA-Z0-9]*$"
I am doing this to replace whitespaces and line breaks. The whitespaces part works however the line break does not.
It works if I removed the tags < > from the br. The line breaks in that case does get replaced by 'br'
I am using Antixss and sanitize.GetSafeHtmlFragment?
Is that causing a problem?
This might work:
http://wpl.codeplex.com/workitem/14053
AntiXss.GetSafeHtmlFragment(strMessage.Replace(ControlChars.Lf.ToString(),"<br>"))
Where strMessage is the string with newlines you're calling AntiXss
on.
This might also help:
http://eksith.wordpress.com/2012/02/13/antixss-4-2-breaks-everything/
.NET AntiXSS with Multiline Textboxes
Maybe a little code....and a description of your current problem
Sounds like your doing this to a models field. In an edit or create view is this correct? if you are using texteditorfor(model => model.property) and the property is of type string then the line breaks will automatically be removed.
If it is a validation error due the HTML it should indicate that on the error page. If this is the case then in the model you must allow html with the [AllowHtml] attribute to the property. you must include system.mvc
Looks like a simple task - get a regex that tests a string for particular length:
^.{1,500}$
But if a string has "\r\n" than the above match always fails!
How should the correct regex look like to accept new line characters as part of the string?
I have a <asp:TextBox TextMode="Multiline"> and use a RegularExpressionValidator to check the length of what user types in.
Thank you,
Andrey
You could use the RegexOptions.Singleline option when validating input. This treats the input as a single line statement, and parses it as such.
Otherwise you could give the following expression a try:
^(.|\s){1,500}$
This should work in multiline inputs.
Can you strip the line breaks before checking the length of the string? That'd be easy to do when validating server-side. (In .net you could use a custom validator for that)
From a UX perspective, though, I'd implement a client-side 'character counter' as well. There's plenty to be found. jQuery has a few options. Then you can implement the custom validator to only run server-side, and then use the character counter as your client-side validation. Much nicer for the user to see how many characters they have left WHILE they are typing.
The inability to set the RegexOptions is screwing you up here. Since this is in a RegularExpressionValidator, you could try setting the options in the regular expression itself.
I think this should work:
(?s)^.{1,500}$
The (?s) part turns on the Singleline option which will allow the dot to match every character including line feeds. For what it's worth, the article here also lists the other RegexOptions and the notation needed to set them as an inline statement.