Connecting Tech Pros Worldwide Forums | Help | Site Map

Disable form submit with illegal words

Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#1: Aug 11 '08
I need a script that can disable the submit button on a form or prevent the form from being submitted when there is an illegal word or character in the text box. This should prevent a spam bot from sending links to me via my website's contact me function. Here is the form that I need to protect:
[HTML]
<form id="FormName" action="thehandler.cgi" method="post" name="FormName">

<p>Name: /Naam: <input type="text" name="name" size="24" /></p>
<p>Email: /E-pos: <input type="text" name="email" size="24" /></p>
<p>Message: /Boodskap:<br />
<textarea name="message" rows="4" cols="30"></textarea></p>
<p><input type="submit" name="Submit/stuur" value="Submit/Stuur" /></p>
</form>
[/HTML]

The message textarea is the field that should be checked. Unnecessary text were removed.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Aug 11 '08

re: Disable form submit with illegal words


It won't prevent spam bots. Someone could just disable JavaScript.

Anyhow, the way to do this is in the normal validation way:
Expand|Select|Wrap|Line Numbers
  1. <form ... onsubmit="return validate()">
then validate() would return false if there are illegal words (and thereby prevent submission) and true otherwise.
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#3: Aug 12 '08

re: Disable form submit with illegal words


Hi, what did you mean by Illegal words, You can validate the form as Mr. Coder suggested or if you want to be more specific. onKeyPress event for TextArea write your own logic, so that the user can only enter character without special symbol and numbers. This alone can be done u can't check what he entered is meaningful or not while the user enters the text over there. If any doubts post it back I will try to help you out

Regards
Ramanan Kalirajan
Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#4: Aug 15 '08

re: Disable form submit with illegal words


Quote:

Originally Posted by RamananKalirajan

Hi, what did you mean by Illegal words, You can validate the form as Mr. Coder suggested or if you want to be more specific. onKeyPress event for TextArea write your own logic, so that the user can only enter character without special symbol and numbers. This alone can be done u can't check what he entered is meaningful or not while the user enters the text over there. If any doubts post it back I will try to help you out

Regards
Ramanan Kalirajan

The illegal words/characters are to be specified in JS and be validated prior to form submit. How do I tell JS to check for it in the text area? That is where my problem is. The characters are: "<>?[]" The idea is to prevent them from sending me links to other sites i don't have time to visit. (these messages are about 120 k each to download and every day there are a lot of them)

Thank you for the help
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Aug 16 '08

re: Disable form submit with illegal words


Use a regular expression, e.g. [<>\?\[\]], to test the input from the text area using the match() method.
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#6: Aug 18 '08

re: Disable form submit with illegal words


Quote:

Originally Posted by Jacotheron

The illegal words/characters are to be specified in JS and be validated prior to form submit. How do I tell JS to check for it in the text area? That is where my problem is. The characters are: "<>?[]" The idea is to prevent them from sending me links to other sites i don't have time to visit. (these messages are about 120 k each to download and every day there are a lot of them)

Thank you for the help

Just check this one. This may the thing what u are looking for

[HTML]<html>
<textarea onkeypress="return checkThis(event)">
</textarea>
</html>
<script type="text/javascript">
function checkThis(e)
{
var ch = e.keyCode;
if(ch!=60&&ch!=62&&ch!=63&&ch!=91&&ch!=93)
return true;
return false;
}
</script>[/HTML]

Regards
Ramanan Kalirajan
Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#7: Aug 21 '08

re: Disable form submit with illegal words


Quote:

Originally Posted by RamananKalirajan

Just check this one. This may the thing what u are looking for

[HTML]<html>
<textarea onkeypress="return checkThis(event)">
</textarea>
</html>
<script type="text/javascript">
function checkThis(e)
{
var ch = e.keyCode;
if(ch!=60&&ch!=62&&ch!=63&&ch!=91&&ch!=93)
return true;
return false;
}
</script>[/HTML]

I tried this script, but for some reason it does not prevent the use of illegal characters and it still gets through.

Thank you for the help
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#8: Aug 22 '08

re: Disable form submit with illegal words


Quote:

Originally Posted by Jacotheron

I tried this script, but for some reason it does not prevent the use of illegal characters and it still gets through.

Thank you for the help

Hello wether the code is showing any error. Or it doesn't restrict the illegal characters. The code was working fine for me. Please tell the problem u faced

Regards
Ramanan Kalirajan
Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#9: Aug 26 '08

re: Disable form submit with illegal words


Quote:

Originally Posted by RamananKalirajan

Hello wether the code is showing any error. Or it doesn't restrict the illegal characters. The code was working fine for me. Please tell the problem u faced

No code error is displayed and the submission is not prevented when entering the illegal characters.

Thank You for the help
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#10: Aug 27 '08

re: Disable form submit with illegal words


Quote:

Originally Posted by Jacotheron

No code error is displayed and the submission is not prevented when entering the illegal characters.

Thank You for the help


I dont know why that code was not working for you. But any how try this one. This amy surely help u out.

[HTML]<html>
<head>
<script type="text/javascript">
var flag=0;
function check(ths)
{
var x = new Array("<",">","?","[","]");
var data = ths.value;
for(var i=0;i<x.length;i++)
{
if(data.indexOf(x[i])>0)
{
alert("coming");
flag=1;
break;
}
flag=0;
}
if(flag==1)
document.getElementById('mysubmit').disabled=true;
else
document.getElementById('mysubmit').disabled=false ;
}
</script>
</head>
<form action="demo.html">
<textarea onkeyup="check(this)">
</textarea>
<br/>
<input type="submit" value="submit" id="mySubmit">
</form>
</html>[/HTML]

Regards
Ramanan Kalirajan
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,132
#11: Aug 27 '08

re: Disable form submit with illegal words


with the suggested regExp by acoder you could cut down the entire thing to the following simple function:

[HTML]<html>
<head>
<script type="text/javascript">
function check(node) {
var re = new RegExp('[<>\?\\[\\]]', 'g');
document.getElementById('mySubmit').disabled = re.test(node.value);
}
</script>
</head>
<form action="demo.html">
<textarea onkeyup="check(this)">
</textarea>
<br/>
<input type="submit" value="submit" id="mySubmit"/>
</form>
</html>[/HTML]
kind regards
Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#12: Aug 28 '08

re: Disable form submit with illegal words


Quote:

Originally Posted by gits

with the suggested regExp by acoder you could cut down the entire thing to the following simple function:

[HTML]<html>
<head>
<script type="text/javascript">
function check(node) {
var re = new RegExp('[<>\?\\[\\]]', 'g');
document.getElementById('mySubmit').disabled = re.test(node.value);
}
</script>
</head>
<form action="demo.html">
<textarea onkeyup="check(this)">
</textarea>
<br/>
<input type="submit" value="submit" id="mySubmit"/>
</form>
</html>[/HTML]

This form works now!

Thank You all for helping e solve this problem

Kind regards
Jacotheron
Reply


Similar JavaScript / Ajax / DHTML bytes