preserving line spaces in asp .net not working - asp.net

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

Related

Removing unwanted characters from username field using infopath

Can anyone tell me how to remove unwanted characters from username field.
ex: i:0#.w|abcventures\sreekiran.k I need to remove the characters which are generated before abcventures\sreekiran.k.
I have used translate() to eliminate those characters, but, it is removing i & w characters also, from the username.
To expand on the current answers, I think they meant to use substring-after(). This will do the trick:
substring-after(userName(), "i:0#.w|")
To expand on Mekalikot's answer -
substring-before(userName(), "i:0#.w|")
Will return just the string after the "i:0#.w|" from the user name function. If the user name function does not include that string, however, the formula will return nothing. Since you'll get a different value from the user name function in preview vs. the browser, you'll probably need to test this with the published form.
Use substring-before to remove those characters.

ValidationExpersion is not a valid attribute of element 'RegularExpressionValidator'

I have this problem: when I'm trying to insert a valid value into Email textbox, I get that this value that was inserted, is invalid. This is how i wrote part of the code in the aspx file:
ValidationExpersion="\w+([-+.']\w+)*#\ w +([-.]\w+)*\.\w+([-.]\w+)*"
This is the message that refer to above code:
Attribute 'ValidationExpersion' is not a valid attribute of element 'RegularExpressionValidator'.
I will be grateful for your help!
Most likely you have a typo in the code (as you do in the question.) The attribute is ValidationExpression so make sure it matches exactly.
EDIT:
Just to make things a little more clear here is the code you pasted and the correct version:
ValidationExpersion="\w+([-+.']\w+)*#\ w +([-.]\w+)*\.\w+([-.]\w+)*"
ValidationExpression="\w+([-+.']\w+)*#\ w +([-.]\w+)*\.\w+([-.]\w+)*"
You spelled ValidationExpression wrong. When programming you have to be accurate with spelling, especially when using built in attributes like ValidationExpression. If you are off by even one letter then the "compiler" will think it's something else.

Regex to limit string length for strings with new line characters

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.

ASP.NET - Negative numbers in Parenthesis

My application is currently displaying negative numbers as -1. The users have changed the requirements (just for a change!) and now we will have to display the numbers as (1).
Can I enable that for the whole application say changing the web.config or even the app's CultureInfo ? Is there any side effect of doing that since we have lots of pages that contain number validators ?
Thanks !
For currency it is really easy:
String.Format("{0:C}", value)
This will use the culture info for the system.
For normal numbers being data bound, use Mark Glorie's sample.
MSDN Article
I'd use String formatting. Making a change to the application's configuration to satisfy a UI requirement is heavy-handed. SteveX wrote a great blog post about String formatting. It's also compatible with markup (aspx) instead of only relevant in code.
From his post:
String.Format(”{0:$#,##0.00;($#,##0.00);Zero}”, value);
This will output “$1,240.00″ if passed 1243.50. It will output the
same format but in parentheses if the number is negative, and will
output the string “Zero” if the number is zero.
Which isn't exactly what you want, but it's close.
Check this..
http://msdn.microsoft.com/en-us/library/91fwbcsb.aspx
Converts the string representation of a number in a specified style to its Decimal equivalent.
I've got the following page bookmarked for doing string formatting: http://idunno.org/archive/2004/14/01/122.aspx
About halfway down, it gives the answer:
String.Format("{0:£#,##0.00;(£#,##0.00);Nothing}", value);
To answer your other question, I wouldn't modify the app.config to make it global, for reasons given in the other answers.
String.Format(”{0:f;(f);0”, -1);
This works.
DataFormatString="{0:c0}"
Nagative amounts in paranthesis
Thousand separater - comma
$ symbol in front
You could always write your own custom ToString() method as an extension method, but like you mention, using CultureInfo is probably better. Take a look here:
http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbernegativepattern.aspx
Are you displaying your data in Gridview/Datagrids? If so then formatting can be applied per bound-column, something like:
<asp:BoundField DataFormatString="{##;(##)}"/>
This only works with integers however...

"Could not reformat the document" in ASP.NET, VS2008

I'm in an ASP.NET UserControl. When I type Control-K, Control-D to reformat all the markup, I get a series of messages from VS 2008:
"Could not reformat the document. The original format was restored."
"Could not complete the action."
"The operation could not be completed. The parameter is incorrect."
Anybody know what causes this?
Edit: OK, that is just...weird.
The problem is here:
<asp:TableCell>
<asp:Button Text="Cancel" runat="server" ID="lnkCancel" CssClass="CellSingleItem" />
</asp:TableCell>
Somehow that asp:Button line is causing the problem. But if I delete any individual attribute, the formatting works. Or if I add a new attribute, the formatting works. Or if I change the tag to be non-self-closing, it works. But if I undo and leave it as-is, it doesn't work.
All I can figure is that this is some sort of really obscure, bizarre bug.
There's probably some malformed markup somewhere in your document. Have you tried it on a fresh document?
Did get the problem today.
My solution: Restart Visual Studio
Usually this sort of behavior is caused by invalid code. It may only be invalid HTML causing it which would still allow the program to be compiled.
For example, if tags are mismatched like this the IDE cannot reformat it.
<div><h1>My Title</div></h1
Check your warnings to see if there are any entries pointing towards mismatched or unclosed tags.
For me, it's usually as issue with whitespace. To fix it, I open Find and Replace (CTRL+H), set Look in to "Current Document", check Use and select "Regular expressions". For Find what I enter ":b|\n" (minus quotes), and for Replace with I enter a single space. Then I click Replace All.
The steps above will replace all whitespace—including line breaks—with a single space, and the next time you format the document, you shouldn't get any errors. That is assuming you don't have malformed HTML.
select the entire suspicious codes segments and use Ctrl+k,Ctrl+F to format only the selected segments instead of whole document .
this way you can find the exact place of problems specially not closed or inappropriate closed tags and fix them .
after all scanning segment by segment is done you can format the whole document for sure
My problem was an extra ". Look carefully the html.
I encountered this for the first time a few weeks ago. I found it was down to invalid HTML. I had to cut out sections of content and paste it back in a little at a time to track down the problem.
For me, I had some bogus characters in my markup code. I only found this out by copy and pasting all my text into Notepad. After that, I saw the bogus characters (showed up as little squares). I just deleted those lines and retyped them and now everything is ok.
I had an unwanted semi-colon. But you may have quote ('), double quote ("), semi-colon (;) or any special character.
So, editing my answer with more details and a screenshot because it still very active.
Go to that line by double clicking the error and search for the extra (unwanted) quote ('), double quote ("), semi-colon (;) or any special character. Remove it because it is causing the error.
Just to add some more information. This issue is caused due to some invalid markup in html.
It won't cause any blocking while running the application.
Unfortunately the solutions mentioned here did not work for me.
1. Restarting visual studio
2. Replacing spaces using regex etc
The best solution to fix the issue is to go to the specific line where the issue is caused and check that line for any invalid symbols like , or ". Just remove it and it will work fine.
My issue is extra " in the value of html attribute, After removing this it is working fine for me.

Resources