PDA

View Full Version : Newbiequestion "recognize word in textline"



Balle
Jun 15th, 2006, 02:42 PM
I'm trying to forbid some words in my form for my guestbook. Thing is I get alot of spam from people who post certain URLs. This URL always contain a couple of characters. I succeded in forbidding this word but I don't know how to forbidd it when it's a part of a complete URL, like this:

http://iliketopost****asspam.com

The word "****" is forbidden but not recognized by the script since it's in a full textline. Here's the script:

<script language="javascript" type="text/javascript">
function VerifyForm(form){
if(myform.txtcomments.value=="****"){
alert("Forbidden letters!");
return false;
}

return true;
}
</script>


AND


<form method="post" OnSubmit="return VerifyForm(this);" action="guestbook.php" name="myform">

Please help me, I'm a complete newbie :)

Kor
Jun 15th, 2006, 04:12 PM
Use Regular Expressions, see:
http://lawrence.ecorp.net/inet/samples/regexp-intro.php

Ex:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var forbidden ='foo';//this is the forbidden word
function verifyForm(f){
var re=new RegExp(forbidden,'g')
if(f['txtcomments'].value.match(re)){
alert('Forbidden letters1');return false
}
}
</script>
</head>
<body>
<form onsubmit="return verifyForm(this)">
<input type="text" name="txtcomments">
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Balle
Jun 15th, 2006, 04:39 PM
Thanks man! :)

Balle
Jun 16th, 2006, 12:16 AM
Grrrr, they posted their URL in the URL input instead..

How can I apply the forbidden word to all input fields?

I have "txtname", "txtemail", "txtwebsite" (<- URL) and "txtcomments".

Thanks for help.

Kor
Jun 16th, 2006, 09:44 AM
<script type="text/javascript">
var forbidden ='foo';//the forbidden word
var names=['txtname','txtemail','txtwebsite','txtcomments'];//array of names
function verifyForm(f){
var re=new RegExp(forbidden,'g')
for(var i=0;i<names.length;i++){
if(f[names[i]].value.match(re)){
alert('Forbidden letters1');return false;
}
}
}
</script>

Balle
Jun 16th, 2006, 11:37 AM
Many thanks! :)

And then if I want to add more forbidden words then I just go:

var forbidden ='foo','something','somethingelse';//the forbidden word

Yes?

Kor
Jun 16th, 2006, 11:39 AM
<script type="text/javascript">
var forbidden =['foo','fee'];//array of forbidden words
var names=['txtname','txtemail','txtwebsite','txtcomments'];//array of names
function verifyForm(f){
for(var i=0;i<names.length;i++){
for(var j=0;j<forbidden.length;j++){
var re=new RegExp(forbidden[j],'g')
if(f[names[i]].value.match(re)){
alert('Forbidden letters!');return false;
}
}
}
}
</script>

Balle
Jun 16th, 2006, 11:46 AM
Thanks again, I wouldn't managed it without your help :)

Kor
Jun 16th, 2006, 11:48 AM
You're welcome. This is the Forums are ment for :thumbsup: