Text Box validator in ASP.NET

Posted by Kpoudyal2010 under ASP.NET on 1/14/2012 | Points: 10 | Views : 1776 | Status : [Member] | Replies : 5
Hi Guys,
i need help on how to make a validate a textbox so user can not enter any especial characters like (~,$,#.&,' * etc ) in my search textbox because my fulltext search query throws exception while i enter the invalid characters, Can you help me how can i over come this situation ?
Thanks




Responses

Posted by: Sksamantaray on: 1/15/2012 [Member] Silver | Points: 25

Up
0
Down

check the char code of each special char and then using javascript function you show error to user, if such character is entered.
or replace the character with space.

otherwise
write a function in your business access class and replace all characters with space before you send it to database to search.
Additionally below the textbox give user in information that special characters are not allowed.

Thanks,
Sanjay

Kpoudyal2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kpoudyal2010 on: 1/15/2012 [Member] Starter | Points: 25

Up
0
Down
my problem is, if user input like : ~business then i should ignore and consider as business only.'
more example: my company's value will be my company value
%ours will be ours only, i need to ignore the special characters and replace with empty character
Thank you and hope you can help me out

Kpoudyal2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 1/15/2012 [Member] Silver | Points: 25

Up
0
Down


public string ReplaceSpecialCharacters(string strData)
{
//If the string is null

if (strData == null)
return strData;

//If the string is empty

if (strData == "")
return strData;

//Replaces single quote (') with apostophe ('`')

// ~, !, @,#,$,%,^,&,*,(),-,_,+,=,;,:,",',

strData = strData.Replace("~", "");
strData = strData.Replace("@", "");
strData = strData.Replace("!", "");
strData = strData.Replace("#", "");
strData = strData.Replace("$", "");
strData = strData.Replace("%", "");
strData = strData.Replace("&", "");
strData = strData.Replace("*", "");
strData = strData.Replace("(", "");
strData = strData.Replace(")", "");
strData = strData.Replace("-", "");
strData = strData.Replace("+", "");
strData = strData.Replace("=", "");
strData = strData.Replace(";", "");
strData = strData.Replace(":", "");
strData = strData.Replace("`", "");
strData = strData.Replace(""", "");
strData = strData.Replace("'", "");
strData = strData.Replace("_", "");
strData = strData.Replace("^", "");
strData = strData.Replace("}", "");
strData = strData.Replace("{", "");
strData = strData.Replace("]", "");
strData = strData.Replace("[", "");
strData = strData.Replace("/", "");
strData = strData.Replace(@"\", "");
strData = strData.Replace(@"|", "");
strData = strData.Replace("'", "");
strData = strData.Replace(">", "");
strData = strData.Replace("<", "");
strData = strData.Replace(".", "");
strData = strData.Replace("?", "");
strData = strData.Replace(":", "");
return strData;
}


you can have the above function in BusinessAccesClass and before you send your text data to database call this function which will remove the special character.I think this will solve your problem.

Thanks,
Sanjay

Kpoudyal2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arefin on: 1/15/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Kpoudyal2010,
You are almost right. Just skip the special characters from search text box, and then apply your like query. It should not create error. Try again.

Thanks,
Arefin

Kpoudyal2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arefin on: 1/15/2012 [Member] Starter | Points: 25

Up
0
Down
Yes, Sksamantaray is right. Just Try....

Thanks,
Arefin

Kpoudyal2010, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response