PDA

View Full Version : certain words in a text box, disable the submit button



lankinator
04-05-2007, 02:17 PM
If someone typed a swear word in a text box - the submit button would then be disabled (straight away) until they removed the word? - how would i do that?

shachi
04-05-2007, 02:19 PM
It depends whether you want the submit button to be disabled as soon as the user types the word or when the user loses focus on the textbox.

lankinator
04-06-2007, 06:16 AM
as soon as the user types the word please.

shachi
04-07-2007, 06:05 AM
attach an onkeydown handler to the textarea and keep checking if the contents of the textarea contains any of the bad words.

jscheuer1
04-07-2007, 06:18 AM
I think onkeyup would be a better choice but, it would be much more efficient, if the submit process is governed by a function, to simply abort the submit function if a bad word was found. Or, have a onmouseover event on the submit button to disable it if off color language was found.

shachi
04-07-2007, 11:00 AM
jscheuer1: yep, the onkeyup handler would certainly be better.

lankinator: But implementation of a bad-word filter with javascript would be a very bad idea(with growing tools like firebug). An example case: if one has stored the bad-words in an array, any person with sufficient javascript knowledge could simply modify the array to be empty which would not lead to a very good filter.

Twey
04-07-2007, 11:08 AM
Or just disable Javascript. It's acceptable as a user interface feature, but the definitive checking should be performed server-side.

shachi
04-07-2007, 11:10 AM
I agree with Twey, I think server-side is the way to go.

jscheuer1
04-07-2007, 11:24 AM
Or just disable Javascript. It's acceptable as a user interface feature, but the definitive checking should be performed server-side.

How will disabling javascript disable the submit button? I agree that the final check to prevent swear words should be done server side. However, I am not aware of any server side method that will disable the submit button at either the moment that a user enters a 'bad' word or at the moment the user moves the mouse to submit.

I'm not saying that it cannot be done, I'm just fairly certain that it cannot be done. I'm always willing to learn new things.

shachi
04-07-2007, 11:29 AM
jscheuer1: I think(but am not sure though), Twey meant that the javascript-swear-word-blocker won't work in cases where javascript is disabled, right Twey?

jscheuer1
04-07-2007, 11:42 AM
jscheuer1: I think(but am not sure though), Twey meant that the javascript-swear-word-blocker won't work in cases where javascript is disabled, right Twey?

Perhaps. But, he didn't make that very clear. In many cases, javascript questions about forms receive the blanket answer, "Do it server side." Often this is the right answer but, I don't think it really is here. Also, if there is no server side fall back to the form to begin with (not the best idea but, acceptable to a designer in certain situations), there is no point in going to the server side to filter data, if there even is a server side language available. Even if there is a server side here, as I said, I don't think it can give the functionality requested. As I also said, if it is available, I agree that it should be used but, as a fall back filter but (if the described functionality is desired at least most of the time), not the only filter.

Twey
04-07-2007, 11:48 AM
How will disabling javascript disable the submit button?You need to take it in context with the above post, the last sentence of which was:
An example case: if one has stored the bad-words in an array, any person with sufficient javascript knowledge could simply modify the array to be empty which would not lead to a very good filter.
Or just disable Javascript.
Even if there is a server side here, as I said, I don't think it can give the functionality requested.Thus, I said it would be acceptable as a UI feature. You're right, in combination with server-side checking a little client-side validation can greatly improve usability.

jscheuer1
04-07-2007, 11:57 AM
I get it. I made the mistake, that many might, of reading the post as though it was a response to the original request. As usual, though not always, Twey, we agree.

boxxertrumps
04-07-2007, 01:12 PM
but there's also a problem of how the words would be checked...
if someone wants the UN "MacRapper", or "TheBallWasHit" they wouldn't be allowed to use it.
(or anything similar, assassin etc...)
it would just impede an innocent user, better to moderate things yourself, a machine can't gage malicious behavior.

Twey
04-07-2007, 01:24 PM
Yeah, there was a problem with that on this board a little while back... one of the scripts had a function called "flashIt" or similar, and so nobody could post about it accurately. I presume ddadmin has disabled this now.

jscheuer1
04-07-2007, 01:30 PM
/\b[aA][sS][sS]\b/

Twey
04-07-2007, 01:33 PM
Fails to catch "assmonkey," amongst other things -- too limited to be useful.

It would be nicer to write that as /\bass\b/i.

mburt
04-07-2007, 02:10 PM
/\bass\b/gi
would be more accurate, wouldn't it?

Also, in PHP, what's wrong with str_replace();?

jscheuer1
04-07-2007, 02:11 PM
The i switch isn't supported in older browsers. You could include as many words as you like:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var bad_words=['My','name','is','mud'];

function makeRegex(str){
var str=str.toLowerCase(), regx='\\b';
for (var i_tem = 0; i_tem < str.length; i_tem++)
regx+='['+str.charAt(i_tem)+str.charAt(i_tem).toUpperCase()+']';
return new RegExp(regx+'\\b');
}

for (var i_tem = 0; i_tem < bad_words.length; i_tem++)
bad_words[i_tem]=makeRegex(bad_words[i_tem]);

function noBad(el){
var subb=el.form.elements;
for (var i_tem = 0; i_tem < subb.length; i_tem++)
if(subb[i_tem].type&&subb[i_tem].type.toLowerCase()=='submit'){
subb=subb[i_tem];
break;
}
for (var i_tem = 0; i_tem < bad_words.length; i_tem++)
if(bad_words[i_tem].test(el.value)){
subb.disabled=1;
return;
}
subb.disabled=0;
}
</script>
</head>
<body>
<form action="#">
<div>
<input type="text" name="bob" onkeyup="noBad(this);">
<input type="submit" value="Go!">
</div>
</form>
</body>
</html>

in the bad_words array.

mburt
04-07-2007, 02:41 PM
I think you're making this too complicated:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>bad word checker</title>
<script type="text/javascript">
var bad_words=['My','name','is','mud'];
var found = false;
function check(el) {
for (var i=0;i<bad_words.length;i++) {
var er = document.getElementById("error");
var reg = new RegExp(bad_words[i].toLowerCase()),value = el.value.toLowerCase();
if (reg.test(value)) {
found = true;
}
}
if (found == true) {
er.style.display="block";
while (f=er.firstChild) er.removeChild(f);
er.appendChild(document.createTextNode("Please do not enter any vulger words."));
return false;
}
else {
er.style.display="none";
window.location.href=el.parentNode.action;
}
}
</script>
</head>
<body>
<form name="myForm" action="http://www.google.ca">
<input name="badwords">
<br><input type="submit" value="button" onclick="check(document.forms['myForm'].badwords);">
<div id="error" style="display:none;color:red"></div>
</form>
</body>
</html>
:D

lankinator
04-07-2007, 02:47 PM
thanks

lankinator
04-07-2007, 02:53 PM
now is there a way i can make the button a submit button but it wont submit the form if a bad word is picked up?

never mind - didnt see the mburt's post :D

jscheuer1
04-07-2007, 03:23 PM
I think you're making this too complicated

How so?

Twey
04-07-2007, 03:37 PM
Still kind of overcomplicated...
<script type="text/javascript">
var curses = new RegExp('\\b' + [
"my",
"name",
"is",
"mud"
].join('\\b|\\b') + '\\b');
</script>

<form action="somepage.php" onsubmit="return this.elements['checkedtext'].onkeyup();">
<input type="text" name="checkedtext" onkeyup="return !(this.form.elements['submit'].disabled = curses.test(this.value.toLowerCase()));">
<input type="submit" name="submit" value="Submit">
</form>
window.location.href=el.parentNode.action;This won't submit any data... you'd want to do el.parentNode.submit().

lankinator
04-07-2007, 04:15 PM
no, no - jscheuer1 one works like a treat - Wouldn't wont to take up any more time that what i have. Thanks :-)

lankinator
05-29-2007, 06:39 PM
I think you're making this too complicated:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>bad word checker</title>
<script type="text/javascript">
var bad_words=['My','name','is','mud'];
var found = false;
function check(el) {
for (var i=0;i<bad_words.length;i++) {
var er = document.getElementById("error");
var reg = new RegExp(bad_words[i].toLowerCase()),value = el.value.toLowerCase();
if (reg.test(value)) {
found = true;
}
}
if (found == true) {
er.style.display="block";
while (f=er.firstChild) er.removeChild(f);
er.appendChild(document.createTextNode("Please do not enter any vulger words."));
return false;
}
else {
er.style.display="none";
window.location.href=el.parentNode.action;
}
}
</script>
</head>
<body>
<form name="myForm" action="http://www.google.ca">
<input name="badwords">
<br><input type="submit" value="button" onclick="check(document.forms['myForm'].badwords);">
<div id="error" style="display:none;color:red"></div>
</form>
</body>
</html>
:D

how would i make that script pick up 2 things (if hello is typed 2 times it disabled the post button and says in the error div, you can only say hello once?) (the word hello is an example :) )