URL format validation in code behind of textbox - c#

I m using one text box, the text box is user choice..
User can paste Text or Url's..
but User want to display a video link... must follow the given validation......
The text box text must have only one video link...
The video link format
"http://www.youtube.com/watch?v=xxxxxxx-xx&feature=g-logo&context=G2093438FOAAAAAAABAA"
(or)
"http://youtu.be/xxxxxxx-xx"
[Note: xxxxxxx-xx -video filename]
3. The above format video only allowed. because user some time
how can i do for this validation? (during validation split functions.....)

is there anything common in your URL?
if so then you can check that string is present in URL or Not
<<**** Add following code in custom validators ServerValidate Event *>>
string str = TextBox1.Text.Trim();
if(str.contains(your_matching_string))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}

A RegularExpressionValidator on the TextBox would do the trick, but you'll need to do some trial and error to come up with the right regex.
Here's a start: "^http://youtu.be/[a-zA-Z0-9]{7}-[a-zA-Z0-9]{2}"
For figuring out the rest, I can at least recommend RadSoftware's Regular Expression Designer.

Related

How to make string bold in code behind

Is there a way to make a string bolder in code behind using C# .NET?
I tried:
string TypeofDay = "<span style='font-weight: bold'>Type Of Day</span> ";
txtbox.Text = TypeofDay + ": " + "Delivery Day"
I am concatenating TypeofDay(bold) and "Delivery Day" to display in a textbox.
You can't make some bits bold and some bits not bold, in an <asp:TextBox>.
You can't make text bolder by enclosing text value in tags. You must change attribute of a control that displays that text for example by setting its CSS class or changing code-behind property:
txbSendMessageBody.Font.Bold = true;
PS. I am concatenating few bold strings and some other strings to display in a textbox.
A HTML text box does not support this. ASP.NET (usually) generates HTML; if HTML does not support this, you cannot solve it from the server side.
A bit hacky alternative can be to use Unicode bold characters
http://qaz.wtf/u/convert.cgi?text=Type+Of+Day
txtbox.Text = "๐—ง๐˜†๐—ฝ๐—ฒ ๐—ข๐—ณ ๐——๐—ฎ๐˜†: ๐–ฃ๐–พ๐—…๐—‚๐—๐–พ๐—‹๐—’ ๐–ฃ๐–บ๐—’"
You could layer a div on top of the textbox. Since the text formatting would change anyway once the user started typing, you can just hide the div once focus is given to the div, thus showing the text box. If nothing is entered and focus is lost, show the div again.
TextBox, no, however you could use a label.
myLabel.text = "<b>bold text</b> normal text <u>underlined text</u> <span style='font-size:Large; color:Red'>Big red text</span>";

What's the correct way to display an error after submitting in asp.net?

I have a textbox, and a submit button. Assuming I do some serverside check, and find the textbox contents are invalid, I want to display an error next to the textbox saying 'invalid text'.
Is there a proper way to do this using validation controls, or do you simply have to stick a label on that you unhide when there's an error?
Edit: Hmm, is there a way to trigger the validation yourself though? I'd rather only do one database query rather than two, if they entered valid data.
You can use CustomValidator
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="invalid text"></asp:CustomValidator>
You need to handle ServerValidate event
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (Condition == true)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
You could always use a custom validator.
What you can use are Validate controls.
You can tie them to a textbox and they perform clientside AND server side validation for you.
For simple validations they work like a charm.
My personal experience with more complex validations are that they don't work as nice as you'd expect.
In that case we perform simple validations with the Validate controls (like mandatory fields etc.) en perform complex validations server side and render a label, like you say.

Add text bullets to a C# form

I am creating a form in C# and need to display text on the form. I need some of the text to show up in a bulleted, unordered list. Is it possible to do this while using a label? Or a rich text box? I am not using ASP.NET and this is for a desktop app.
My message should look like this:
To continue please selected one of the actions below:
Click ButtonA to do this action.
Click ButtonB to do this action.
Thanks!
Just to add as a suggestion to the OP, I have found that using a StringBuilder to construct multi-line messages helps me keep the formatting straight:
StringBuilder messageBuilder = new StringBuilder(200);
messageBuilder.Append("To continue, please select one of the actions below:");
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append("\t\u2022 Click Button A to do this action.");
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append("\t\u2022 Click Button B to do this action.");
MessageBox.Show(messageBuilder.ToString());
You can use ASCII or UniCode characters, and reformat your string to replace a marker character with the formatting character.
Say you defined your string like so:
var myMessage = "To continue please selected one of the actions below: * Click ButtonA to do this action. * Click ButtonB to do this action."
you could do a Regex.Replace that looked for asterisks and converted to bullets:
var formattedString = Regex.Replace(myMessage, "\*", "\r\n \u2022");
Since the string was defined on one line, we also put in a newline ("\r\n").
If you'll take a look at the Character Map (Start > Programs > Accessories > System Tools > Character Map), you can locate the keystroke "code" for most characters.
For Arial Text, the "Middle Dot" keystroke combination is:
Alt+0183
To place this in your text using Visual Studio, hold down the Alt key while typing in "0183" on the Numeric Keypad (not the keys over the alpha pad).
This should give you "ยท" in your text.
That's as close as I have come.
Note that other special characters (degrees - Alt+0176 ยฐ, copyright - Alt+0169 ยฉ, and many others) can be included with this technique.
Yes you can use text below:
const string text = #"To continue please selected one of the actions below:
. Click ButtonA to do this action.
. Click ButtonB to do this action.";
label1.Text = text;
You can use special characters for the bullet as well such as *.
You could use a WebBrowser control. I.E.:
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.DocumentText 1 = "<li>Click ButtonA ...</li><li>Click ButtonB ...</li>"

asp.net formatting an email

I am working on an application that involves the user clicking on a hyperlink to popup an outlook email template. I have the SendTo values, and the subject values.
How do I enter text into the body of the email?
In the hyperlink you can add the ?body= variable after the email address. Formatting the body will be a challenge because for symbols like spaces you have to use hex codes (space = %20, newline = %0D). I don't know what your design criteria are, but most of the time I prefer to create a form with the desired input fields and let .NET handle the sending of the e-mail message on the submit.
Are you searching for this?
...

How to handle Html inputs in the TextBox

I have a requirement that user can input HTML tags in the ASP.NET TextBox. The value of the textbox will be saved in the database and then we need to show it
on some other page what he had entered. SO to do so I set the ValidateRequest="false" on the Page directive.
Now the problem is that when user input somthing like :
<script> window.location = 'http://www.xyz.com'; </script>
Now its values saved in the database, but when I am showing its value in some other page It redirects me to "http://www.xyz.com" which is obvious
as the javascript catches it. But I need to find a solution as I need to show exactly what he had entered.
I am thinking of Server.HtmlEncode. Can you guide me to a direction for my requirement
Always always always encode the input from the user and then and only then persist in your database. You can achieve this easily by doing
Server.HtmlEncode(userinput)
Now, when it come time to display the content to the user decode the user input and put it on the screen:
Server.HtmlDecode(userinput)
You need to encode all of the input before you output it back to the user and you could consider implementing a whitelist based approach to what kind of HTML you allow a user to submit.
I suggest a whitelist approach because it's much easier to write rules to allow p,br,em,strong,a (for example) rather than to try and identify every kind of malicious input and blacklist them.
Possibly consider using something like MarkDown (as used on StackOverflow) instead of allowing plain HTML?
You need to escape some characters during generating the HTML: '<' -> <, '>' -> >, '&' -> &. This way you get displayed exactly what the user entered, otherwise the HTML parser would possibly recognize HTML tags and execute them.
Have you tried using HTMLEncode on all of your inputs? I personally use the Telerik RadEditor that escapes the characters before submitting them... that way the system doesn't barf on exceptions.
Here's an SO question along the same lines.
You should have a look at the HTML tags you do not want to support because of vulnerabilities as the one you described, such as
script
img
iframe
applet
object
embed
form, button, input
and replace the leading "<" by "& lt;".
Also replace < /body> and < /html>
HTML editors such as CKEditor allow you to require well-formed XHTML, and define tags to be excluded from input.

Categories

Resources