Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I have one page where I am creating email content body, but some clients are copied content from excel sheet to text area and some special characters are coming during copy paste which are not showing in text area but when I save the content by web service method, its throwing an exception of Bad Request, I debug the code and found that some special characters like [I am not able to paste here that char] coming from sheet.

How to avoid that kinds of character in text area? Which encoded charset or attribute to be used to avoid such a chars?

Thanks
Imrankhan
Posted
Comments
Brian A Stephens 11-Oct-13 15:59pm    
You tagged this question with HTML5 only, not Javascript. With HTML5 only, the closest you'll get is the new pattern attribute of input tags. It will prevent any characters that don't match the specified regex.

But if you want to allow the copy/paste, but clean it for the user, you'll need Javascript or server-side code. Write a JS function that utilizes regex to replace the invalid characters, bound to the onkeypress event.

1 solution

Hello,
Use this function
public static string RemoveSpecialCharacters(string str) {
   StringBuilder sb = new StringBuilder();
   foreach (char c in str) {
      if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_') {
         sb.Append(c);
      }
   }
   return sb.ToString();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900