Current location: Hot Scripts Forums » General Web Coding » JavaScript » Please help, scanning through a textarea.


Please help, scanning through a textarea.

Reply
  #1 (permalink)  
Old 09-07-07, 10:28 AM
Zefer's Avatar
Zefer Zefer is offline
Wannabe Coder
 
Join Date: May 2007
Posts: 190
Thanks: 2
Thanked 0 Times in 0 Posts
Please help, scanning through a textarea.

Okay, well I have an Array of "bad words" that cannot post, so I need a code that scans through the the textarea to find any "bad words" and if it does it runs a function... Thanks...
__________________
I know how to code in: JavaScript, PHP, (x)HTML, CSS, QuickBasic, VisualBasic
I'm learning to code in: C++ and Python

If my post was helpful, press the thanks button below.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-07-07, 10:39 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
how about this (there might be an easier way to "implode" the array, but i'm not very familiar with the javascript array functions):
Javascript Code:
  1. function implode ( my_array ) {
  2.   var to_be_returned = "";
  3.   for ( var i=0; i<my_array.length; i++ ) {
  4.     to_be_returned = my_array[i] + "|";
  5.   }
  6.   // there's a | too much at the end, so we need to remove it first:
  7.   return to_be_returned.substring ( 0, to_be_returned.length - 1 );
  8. }
  9.  
  10. function checkForInvalidWords ( obj ) {
  11.   var result = obj.value.search ( "(" + implode (array_of_bad_words) + ")");
  12.   if ( result == null ) {
  13.     // valid words
  14.   } else {
  15.     // invalid words found
  16.   }
  17. }
and the html:
HTML Code:
<textarea name="text" onblur="checkForInvalidWords(this)"></textarea>
The "result" variable will hold an array with all matched invalid words, so you can simply use the string.replace method to replace them with a different word or censure them
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-07-07, 10:58 AM
Zefer's Avatar
Zefer Zefer is offline
Wannabe Coder
 
Join Date: May 2007
Posts: 190
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks!
__________________
I know how to code in: JavaScript, PHP, (x)HTML, CSS, QuickBasic, VisualBasic
I'm learning to code in: C++ and Python

If my post was helpful, press the thanks button below.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-07-07, 11:05 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
It's join(), in JavaScript.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-08-07, 08:52 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
On a sidenote, I often use a similar method to check if a string exists in an array of strings by making the stringArray a Regular Expression.
It's especially useful if the array changes dynamically between tests.
^ means the beginning of the testString, $ means the end, and | means "or".
The "i" means it ignores upper/lower case.
javascript Code:
  1. var theStrings = ['String A','String B','String C'];
  2. function exists(testString)
  3. {
  4.   return new RegExp("^"+theStrings.join("$|^")+"$","i").test(testString);
  5.   // Evaluates to the same as /^String A$|^String B$|^String C$/i.test(testString)
  6. }
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Textarea defined by td ? Nervosa CSS 3 06-18-07 01:06 AM
Editing text in Textarea html control sudidelaravindra JavaScript 1 09-15-06 11:17 AM
script subtratct input from textarea and display the result in othe textarea mathfxr Script Requests 2 12-19-04 12:19 PM


All times are GMT -5. The time now is 01:23 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.