Need help with Javascript

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Need help with Javascript

 
0
  #1
Jan 15th, 2007
Hi folks, I need some help with A javascript thats basically a spam/swearword filter. Its designed to disable the submit button if a matching word from an array is found.

heres a working example

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript1.2">
  2.  
  3. // Word Filter
  4.  
  5.  
  6. var swear_words_arr=new Array("silly","crap","idiot","http");
  7. var swear_alert_arr=new Array;
  8. var swear_alert_count=0;
  9.  
  10. function reset_alert_count()
  11. {
  12. swear_alert_count=0;
  13. }
  14.  
  15. function validate_text()
  16. {
  17. reset_alert_count();
  18. var compare_text=document.formx.post.value;
  19. for(var i=0; i<swear_words_arr.length; i++)
  20. {
  21. for(var j=0; j<(compare_text.length); j++)
  22. {
  23. if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).t
  24. oLowerCase())
  25. {
  26. swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i
  27. ].length));
  28. swear_alert_count++;
  29. }
  30. }
  31. }
  32. var alert_text="";
  33. for(var k=1; k<=swear_alert_count; k++)
  34. {
  35. alert_text+="*" + "(" + k + ") " + swear_alert_arr[k-1];
  36. }
  37. if(swear_alert_count>0)
  38. {
  39. alert("Your post cannot be submitted.*The following Naughty words were found:*_______________________________*" + alert_text + "*_______________________________");
  40. document.formx.post.select();
  41. }
  42. else
  43. {
  44. document.formx.submit();
  45. }
  46. }
  47.  
  48. function select_area()
  49. {
  50. document.formx.post.select();
  51. }
  52.  
  53. window.onload=reset_alert_count;
  54.  
  55. </script>
  56.  
  57.  
  58.  
  59. <form name="formx" method="post" action="post">
  60.  
  61. <textarea rows="3" cols="40" name="post" style="border:2 solid #808080; font-family:verdana,arial,helvetica; font-weight:normal; font-size:10pt" onclick="select_area()">Enter your text here...</textarea>
  62.  
  63. <input type="button" style="background:#EFEFEF; border:2 solid #808080; width:100px; cursor:pointer" value="Submit" onclick="validate_text();">
  64. </form>

test here

http://leprofesseur.tripod.com/html-testbed.htm

Im trying to intergrate this into my ipb v1.3 forum for a specific forum only (guests). Having no luck despite my efforts. Heres what I tried (matching my system) but it still submits. Not sure how to 'href. match' for a specific forum url only.



JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript1.2">
  2.  
  3. // Cant even post bad words Filter
  4.  
  5. var swear_words_arr=new Array("WORD1","WORD2","WORD3");
  6. var swear_alert_arr=new Array;
  7. var swear_alert_count=0;
  8.  
  9. function reset_alert_count()
  10. {
  11. swear_alert_count=0;
  12. }
  13.  
  14. function validate_text()
  15. {
  16. reset_alert_count();
  17. var compare_text=document.REPLIER.post.value;
  18. for(var i=0; i<swear_words_arr.length; i++)
  19. {
  20. for(var j=0; j<(compare_text.length); j++)
  21. {
  22. if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).t
  23. oLowerCase())
  24. {
  25. swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i
  26. ].length));
  27. swear_alert_count++;
  28. }
  29. }
  30. }
  31. var alert_text="";
  32. for(var k=1; k<=swear_alert_count; k++)
  33. {
  34. alert_text+="*" + "(" + k + ") " + swear_alert_arr[k-1];
  35. }
  36. if(swear_alert_count>0)
  37. {
  38. alert("Your post cannot be submitted.*The following Naughty words were found:*_______________________________*" + alert_text + "*_______________________________");
  39. document.REPLIER.post.select();
  40. }
  41. else
  42. {
  43. document.REPLIER.submit();
  44. }
  45. }
  46.  
  47. function select_area()
  48. {
  49. document.REPLIER.post.select();
  50. }
  51.  
  52. window.onload=reset_alert_count;
  53.  
  54. </script>
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. in submit input button put....
  63. onclick="validate_text()"
  64.  
  65. in textarea put....
  66. onclick="select_area()">

Any help appreciated thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Need help with Javascript

 
0
  #2
Jan 16th, 2007
Hi,[HTML][/HTML]

You should put some alerts in your code to see if it is actually being run or not, and to see if the variables are changing as expected...

You could probably just check word for word also by creating an array of words out of the form field value.

eg:

var arr = document.formx.post.value.split(/[\s\.,\-]+/);

Then matching the words in the array to each "spam word".

Here's an prototype of the Array() that returns the index of a matched value..

[HTML]Array.prototype.in_array = function(value) {
for (var x = 0; x < this.length; x++) {
if (this[x] == value) {
return x;
}
}
return -1;
};[/HTML]

eg:

[HTML]
...
if (swear_words_arr.in_array(word.toLowerCase()) != -1) {
// spam word found
}
...[/HTML]

That way you don't have to match character for character...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Need help with Javascript

 
0
  #3
Jan 16th, 2007
Thanks. For some reason I just tested it in a testbed (my 'working' example *cough* and it didnt work! ?? help clueless!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Need help with Javascript

 
0
  #4
Jan 16th, 2007
Originally Posted by Inny View Post
Thanks. For some reason I just tested it in a testbed (my 'working' example *cough* and it didnt work! ?? help clueless!
You mean when you put in the alerts they were never fired?

Regarding:

[HTML]onclick="validate_text()"[/HTML]

Is this onclick on an input of type="button" or type="submit" ? If its of type="submit" then the form every time..
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Need help with Javascript

 
0
  #5
Jan 16th, 2007
You shouldn't use JavaScript for restrictive/non-helpful form validation. It's amusingly easy to bypass.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Need help with Javascript

 
0
  #6
Jan 16th, 2007
Its a submit.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Need help with Javascript

 
0
  #7
Jan 16th, 2007
An onsubmit event handler would probably be more use, and it's a form event rather than a button/input event.

http://www.htmlcodetutorial.com/form..._onSubmit.html
Last edited by MattEvans; Jan 16th, 2007 at 1:26 pm. Reason: grammar
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Need help with Javascript

 
0
  #8
Jan 16th, 2007
Arrrrrgh! Thanks people I appreciate your input but Im just too daft to make the thing work im afraid. My knowledge is very very basic.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Need help with Javascript

 
0
  #9
Jan 17th, 2007
don't give up yet! You're so close..

Since the onclick event listener is attached to a submit field rather than just a button, the form will be submitted no matter what. Your onclick event listerner will most likely not complete execution.

What you can do is change the field to a type="button" instead of type="submit".
Then the form will not be submitted unless you submit it via javascript with the form.submit() method.

However, a better way of doing it is to attach an event Listener to the form submit method, as Matt said.
This allows a user without javascript on their browser to submit the form, but users with javascript to have the form validation.

You could do it like:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.formx.onsubmit = function() {
  2.  
  3. // paste the form validation code you have here..
  4. // then use "return false;" to not send the form, and "return true;" to send it.
  5.  
  6. };

A better way to attach event listerners is to use the W3C DOM method: addEventListener()

http://developer.mozilla.org/en/docs...dEventListener

and use the M$ implementation, attachEvent(), where needed.

Heres one implementation:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. addEvent = function(el, evType, fn, useCapture) {
  2. if (el.addEventListener) {
  3. el.addEventListener(evType, fn, useCapture);
  4. return true;
  5. }
  6. else if (el.attachEvent) {
  7. var r = el.attachEvent('on' + evType, fn);
  8. return r;
  9. }
  10. else {
  11. el['on' + evType] = fn;
  12. }
  13. }

that way you can attach multiple events, and also not overwrite any events that are attached by other parts of the script.

Off course when you use JS form validation, it should be considered as helping the form user, nothing more.
I wrote something on this last night: http://fijiwebdesign.com/content/view/93/77/ (its not finished)
JS form validation is very useful in helping the user out however since close to 100% of users have JS on their browser.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Need help with Javascript

 
0
  #10
Jan 17th, 2007
Thanks fopr the encouragement!
this does indeed work well (see below) but Im not sure how to integrate/make changes to my form (below that) to work.

[please assist if you havent run out of patientts

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript1.2">
  2. var swear_words_arr=new Array("silly","bloody","idiot","*******");
  3. var swear_alert_arr=new Array;
  4. var swear_alert_count=0;
  5.  
  6. function reset_alert_count()
  7. {
  8. swear_alert_count=0;
  9. }
  10.  
  11. function validate_user_text()
  12. {
  13. reset_alert_count();
  14. var compare_text=document.form1.user_text.value;
  15. for(var i=0; i<swear_words_arr.length; i++)
  16. {
  17. for(var j=0; j<(compare_text.length); j++)
  18. {
  19. if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase())
  20. {
  21. swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length));
  22. swear_alert_count++;
  23. }
  24. }
  25. }
  26. var alert_text="";
  27. for(var k=1; k<=swear_alert_count; k++)
  28. {
  29. alert_text+="\n" + "(" + k + ") " + swear_alert_arr[k-1];
  30. }
  31. if(swear_alert_count>0)
  32. {
  33. alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________");
  34. document.form1.user_text.select();
  35. }
  36. else
  37. {
  38. document.form1.submit();
  39. }
  40. }
  41.  
  42. function select_area()
  43. {
  44. document.form1.user_text.select();
  45. }
  46.  
  47. window.onload=reset_alert_count;
  48.  
  49. </script>
  50.  
  51.  
  52.  
  53. <form name="form1" method="post" action="your_post_address.asp">
  54.  
  55. <table><tr><td></td></tr></table>
  56. <textarea rows="3" cols="40" name="user_text" style="border:2 solid #808080; font-family:verdana,arial,helvetica; font-weight:normal; font-size:10pt" onclick="select_area()">Enter your text here...</textarea>
  57. <table><tr><td></td></tr></table>
  58. <center><input type="button" style="background:#EFEFEF; border:2 solid #808080; width:100px; cursor:pointer" value="Submit" onclick="validate_user_text();"></center>
  59. </form></form>


my form that needs changing...(relavent Parts)

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <textarea cols='70' rows='8' name='Post' class='textinput' tabindex="1">
  3. </textarea>
  4. <BR>
  5. <BR>
  6. <a href="http://www.upload3r.com/"target="_blank">Alternate upload Click here (paste url)</a>
  7. <BR>
  8. <BR>
  9. {ibf.lang.upload_title}Attach single image
  10. {ibf.lang.upload_text} $data<input class='textinput' type='file' size='30' name='FILE_UPLOAD' /><BR><BR>
  11.  
  12. <input type='checkbox' name='enableemo' value='yes' class="checkbox" checked="checked" />&nbsp;{ibf.lang.qr_add_smilie} |
  13. <input type='checkbox' name='enablesig' value='yes' class="checkbox" checked="checked" />&nbsp;{ibf.lang.qr_add_sig}
  14.  
  15. </TD></TR>
  16. </Table>
  17.  
  18. <div align='center'>
  19. <input type='submit' name='submit' value='Submit' class='forminput' tabindex="2" accesskey="s" />&nbsp;
  20. <input type='submit' name='preview' value='{ibf.lang.qr_more_opts}' class='forminput' />
  21. &nbsp;&nbsp; <input type='button' name='qrc' onclick="ShowHide('qr_open','qr_closed');" value='{ibf.lang.qr_closeit}' class='forminput' />
  22. </div>
  23. </div>
  24. </form>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC