473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

disallow special chracter and list of words

Hi All,

I would like to know what's the best way to write function(global) in asp.net c# Framework 2.0 to check for textbox in a form (i have about 80 textbox on one form and I have many forms and textarea
and I have list of 90 ro 100 words and special chracter that I do not want user to enter. If they enter the item from the list of blocked word or character I want to give them error saying "this character or word in not allowed, please correct it"
The site is getting many hits already so I want to make sure it's not going to slow down a lot. I want to give the message as soon as one of the word or special character listed is found and not want to continue checking further.

e.g of block list word "abc","bbc","bbd","cbc","xbc","~","^","'". the list can change in the future.

I am new to asp.net and appreciate any help.

Thanks
newcoder
Dec 13 '09 #1

✓ answered by semomaniz

here is the utility class, you go to add more data and any other validation that you require. The isvalid method returns boolean and is used to validate the data. now on the button event you will validate the data if it returns false then display your desired error

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections;
  11.  
  12. public class Utility
  13. {
  14.     //data member
  15.     private string _InputText;
  16.  
  17.     //constructor
  18.         public Utility() { }
  19.     public Utility(string datatovalidate)
  20.     {
  21.         this.ValidationText = datatovalidate;
  22.     }
  23.  
  24.     //getter n setter
  25.     public string ValidationText
  26.     {
  27.         get { return _InputText; }
  28.         set { _InputText = value; }
  29.     }
  30.  
  31.     //method    
  32.     public bool IsValid()
  33.     {
  34.         bool result = false;
  35.         //creating ArrayList 
  36.         ArrayList mydata = new ArrayList();
  37.  
  38.         myData.Add("abc");
  39.         myData.Add("ggc"); 
  40.         //now add all the unwanted data here
  41.  
  42.  
  43.         foreach (string x in myData)
  44.         {
  45.             if ( x.Equals(this.ValidationText))
  46.             {
  47.                 return result;
  48.             }
  49.             else
  50.             {
  51.                 result = true;
  52.             }
  53.         }
  54.         return result;
  55.     }
  56.  
  57. }
  58.  
  59.  

17 2041
sanjib65
102 100+
The simplest way that comes to my mind is using if/else constructor where you have to mention those charaters and words.
Dec 14 '09 #2
Frinavale
9,735 Expert Mod 8TB
I typically have a "Utils" class that just has a bunch of public static/shared methods that help me do things like print to a log etc.

I seen no reason why you can't use such a class in your application to do validation. You can call these methods from anywhere in your application.

-Frinny
Dec 14 '09 #3
your are right. Honestly I am very new to this and I do not know how to right such a function. Any help in sarting to write this function would be great.

Thanks
Dec 14 '09 #4
semomaniz
210 Expert 100+
Create an array and store all the words inside the array. Then use a for each loop and do the validation rather than using if else statement. Also as Frinny stated create a utility class to do the validation, This makes code much cleaner and more oop.
Dec 14 '09 #5
Hi semomaniz,

I am new to this and I do not know how to create array. I searched all over the internet and I could not find one. I found a few that will replace the bad word or special character but I do not want to filter. I just want to give the message saying this word is not allowed. I just need someone to help me give an example and I will enter all the words.

Thanks for your help
Dec 14 '09 #6
semomaniz
210 Expert 100+
here is the utility class, you go to add more data and any other validation that you require. The isvalid method returns boolean and is used to validate the data. now on the button event you will validate the data if it returns false then display your desired error

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections;
  11.  
  12. public class Utility
  13. {
  14.     //data member
  15.     private string _InputText;
  16.  
  17.     //constructor
  18.         public Utility() { }
  19.     public Utility(string datatovalidate)
  20.     {
  21.         this.ValidationText = datatovalidate;
  22.     }
  23.  
  24.     //getter n setter
  25.     public string ValidationText
  26.     {
  27.         get { return _InputText; }
  28.         set { _InputText = value; }
  29.     }
  30.  
  31.     //method    
  32.     public bool IsValid()
  33.     {
  34.         bool result = false;
  35.         //creating ArrayList 
  36.         ArrayList mydata = new ArrayList();
  37.  
  38.         myData.Add("abc");
  39.         myData.Add("ggc"); 
  40.         //now add all the unwanted data here
  41.  
  42.  
  43.         foreach (string x in myData)
  44.         {
  45.             if ( x.Equals(this.ValidationText))
  46.             {
  47.                 return result;
  48.             }
  49.             else
  50.             {
  51.                 result = true;
  52.             }
  53.         }
  54.         return result;
  55.     }
  56.  
  57. }
  58.  
  59.  
Dec 14 '09 #7
Frinavale
9,735 Expert Mod 8TB
@semomaniz:
Since newcoder10 doesn't even know how to declare an array I don't think that your class is going to make sense to them.

@newcoder10:
You are going to have to do your part in learning the basics. Knowing how to declare an array is extremely basic..in fact I'm not even sure what tutorials would be good for you because most articles out there assume that you know things like how to declare an array, how to use if statements, and how to use loops. Another reason why I don't know where to point you to is because you haven't stated what programming language you're using to develop your sever side logic. Are you using C#? Are you using VB.NET?

-Frinny
Dec 14 '09 #8
Hi semomaniz,

Thanks for your help. I will play with this. I reall appreciate the code.

Dear Frinny, Thanks for your comments I am doing my part in learning and appreciate your willingness to direct me to tutorial. BTW I did mentioned above "asp.net c# Framework 2.0 " Sorry if my english isn't that well.

Thanks again to all of you.
Dec 14 '09 #9
Frinavale
9,735 Expert Mod 8TB
Sorry newcoder10... I was concentrating more on your problem and over looked that.

Just a sec and I'll find you some simple tutorials to help you learn C#.

-Frinny
Dec 14 '09 #10
Hi Frinny,

Please no need to apologize, I should be thankful to your comments and help. I am glad I found and joined the site. I am sure I will learn a lot from all you gurus.
Once again thanks a lot for your patience, for helping and directing me to right path.

Thanks again.
Dec 14 '09 #11
Frinavale
9,735 Expert Mod 8TB
The best place to start learning anything about .NET is on the MSDN Library. You should be able to use the library's search option to find help on just about anything that you want to learn about. This resource is very powerful, it has articles on every topic and every control. I use it almost every day (especially when I'm doing research). I recommend bookmarking it and using it as your primary resource when doing any development in .NET

There are tutorials there on how to use Arrays in C#.

I also recommend that you check out:
It's important to understand the ASP.NET life cycle but first you need to understand the basics: arrays, logical conditional statements, classes and events.

**edit**
Ooo this one looks good too C# Programming Guide


-Frinny
Dec 14 '09 #12
Frinny,

Thanks again for all the help. I will try to limit asking basic questions and will read and research before posting.

Thanks alot.
Dec 14 '09 #13
Frinavale
9,735 Expert Mod 8TB
:) If you need help understanding something feel free to ask too :)
It's easier to help you if you have some sort of starting point...like a chunk of code or something...as apposed to "how do I do validation", which could be a huge topic.

-Frinny
Dec 14 '09 #14
Hi semomaniz,

I created the class file and it looks ok. I had one error message about case on myData and I changed that it does not have any error, I am trying to call this method on click event and not sure how to do it.
Again I am sorry for asking this basic question.

Thanks
Dec 14 '09 #15
semomaniz
210 Expert 100+
first you will need to include that class in the project. Make sure that class is in your App_Code Folder. Add namespace on the class. then on the page code file add a reference to the namespace then you should be able to call the methods of the class.

Please google for C# OOP practices. You will understand how class works and how to call the methods.

If you do not understand please let us know.
Dec 14 '09 #16
Frinavale
9,735 Expert Mod 8TB
You need to create an instance of the class that semomaniz posted in order to use it's methods.

This goes back to the basics on classes and objects. A class is like a blue print for a house: it defines the rooms, doors, and properties of the house. An instance of the class is the actual house. You cannot use the rooms and doors of the house until you create an instance of it.

So, like I was saying, you need to create an instance of the class that semomaniz posted:
Expand|Select|Wrap|Line Numbers
  1. Utility validatorUtility;
  2. validatorUtility = new Utility(); //<--creates a new instance of the class
Now you can use the properties and methods:
Expand|Select|Wrap|Line Numbers
  1. Utility validatorUtility;
  2. validatorUtility = new Utility(); //<--creates a new instance of the class
  3.  
  4.  //Setting the ValidationText property  of the vaidatorUtility Utility Object
  5. validatorUtility.ValidationText = myTextBox.Text;
  6.  
  7. //Now that you have set the ValidationText property 
  8. //of the validatorUtility Utility Object you can validate the text
  9.  
  10.  
  11. //To validate the text you call the validate method:
  12. if(validatorUtility.IsValid){
  13.   //the text is valid
  14. }else{
  15.   //the text is not valid
  16. }
  17.  
What I was suggesting was something a little different different.
I was suggesting that you the Utility class only contains static methods. That way you wouldn't have to create an instance of the class every time you want to validate something.

The class would look like:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4.  
  5. public static class Utility
  6. {
  7.     //constructor
  8.     private static Utility() { }
  9.  
  10.     //method    
  11.     public static bool IsValid(string textToValidate)
  12.     {
  13.         bool result = false;
  14.         //creating a list of strings that are 
  15.        //VALID entries
  16.         ArrayList blacklist = new ArrayList();
  17.  
  18.        //now add all the VALID options to the whiteList ArrayList
  19.         whitelist.Add("abc");
  20.         whitelist.Add("ggc"); 
  21.  
  22.  
  23.         foreach (string validOption in whitelist)
  24.         {
  25.             if ( String.Compare(validOption, textToValidate, true) == 0 )
  26.             {
  27.                 return result = true;
  28.             }
  29.         }
  30.         return result;
  31.     }
  32.  
  33. }
I changed the proposed validation method to use a whitelist instead of a blacklist. A whitelist is a list of valid options that the user can provide. A black list is a list of invalid options. It is much easier to maintain a list of valid options instead of a list of invalid options....

Anyways....to use my validation method you would not have to create an instance of the class. You'd just use the method:
Expand|Select|Wrap|Line Numbers
  1. if(Utility.IsValid(myTextBox.Text)){
  2.   //the text is valid
  3. }else{
  4.   //the text is not valid
  5. }
  6.  
Static classes and methods are advanced topics though.
So for now I would recommend using semomaniz's class since it's easier to understand for now. I would probably change semomaniz's validation method to use a whitelist though ;)

-Frinny
Dec 14 '09 #17
Frinny,

You are Great!, I was struggling a bit with semomaniz code and after a while I tried your code, and with couple minor changes I was able to get it to work, it not only works great but I do not see any performance issues with adding 50 + words so far.
Once again Frinny and semomaniz thanks for all your help. I have been working on this since Friday and finally got it to work exactly I wanted.
You guys are the best, Please don't go too far, I will be back soon :-)

My Sincere Thanks,
newcoder10
Dec 14 '09 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
5
by: Roy Smith | last post by:
I've got a silly little problem that I'm solving in C++, but I got to thinking about how much easier it would be in Python. Here's the problem: You've got a list of words (actually, they're...
3
by: ATH0 | last post by:
How to search for special character { } and how to count them.. I got field called text ( undefined length ) and in this field you must define "{" as start and "}" as end of some text line. If...
10
by: barcaroller | last post by:
Can anyone tell me what the difference is between the 3rd Edition and the Special Edition of Stroustrup's "The C++ Programming Language"?
26
by: Swroteb | last post by:
Hi there, I've got a reasonably sized list of objects that I'd like to pull out all combinations of five elements from. Right now I have a way to do this that's quite slow, but manageable. I...
1
by: yawgmoth7 | last post by:
Hello, I have a piece of code: command = raw_input("command> ") words = string.split(command, ' ') temparg = words if len(words)<= 3: temparg = words else: temparg = words funcarg =...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
1
by: bdparnes | last post by:
I have a project where it is necessary to copy rows (and change a few pieces of information); however some of the rows may contain special characters such as an apostrophie or quotes. I am using the...
1
by: newcoder10 | last post by:
I am sorry I am posting this twice. I originally posted this under asp.net and then read the comment on top. My sincere aplogy. I beleive this question goes here. Hi All, I would like to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.