Friday, July 9, 2010

Remove html ,show message alert, encode and decode string in html

Check text is valid or not


If you want to check that your text is valid text or not no problem just call this function and your work is done.
private bool IsTextValidated(string strTextEntry)
{
Regex objNotWholePattern = new Regex("[^0-9]");
return !objNotWholePattern.IsMatch(strTextEntry) && (strTextEntry != "");
}

Remove HTML tags from a string


If you want to remove all HTML tags from your input string use this function and your work is done.

private string StripHTML(string htmlString)
{
string pattern = @"<(.|\n)*?>";
return Regex.Replace(htmlString, pattern, string.Empty);
}

Encode a string into a valid HTML format


public string replaceHtmlEncode(string desc)
{
String EncodedString = Server.HtmlEncode(desc);
return EncodedString;
}

Decode string into a valid HTML format


public string replaceHtmlDecode(string desc)
{
String DecodeString = Server.HtmlDecode(desc);
return DecodeString;
}

Show a alert using c# and javascript


If you do not want to show message on label and want to a alert on browser than
the below function will do this for you.It just creates a new object of label and than set the text property of label to a javascript alert and adds it to the collection of controls in page on runtime.

public void MsgBox(string Message)
{
Label lbl = new Label();
lbl.Text = "<script type='text/javascript'>alert('" + Message + "'); </script>";
Page.Controls.Add(lbl);
}

Convert a string to title case


If you want that your string will be in Title case so that it can shown properly in Navigation or in heading .Use this function it will convert any string to tile case

public string converttotitlecase(string str)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
return textInfo.ToTitleCase(str)
}

No comments:

Post a Comment

Popular Posts