/    Sign up×
Community /Pin to ProfileBookmark

Need some help on form validation

What’s up fellas? I’m trying to validate a form using a word filter script I found on the net and a blank message validator. I want to somehow combine the scripts. What’s happening now is if I post a blank message the alert will come up but the form will process anyway… same with the word filter. is there a way to fix this so it doesn’t process the form?

[code]
<SCRIPT LANGUAGE=”JavaScript”>
var swear_words_arr=new Array(“bloody”,”war”,”terror”);

var swear_alert_arr=new Array;
var swear_alert_count=0;
function reset_alert_count()
{
swear_alert_count=0;
}
function validate_message()
{
reset_alert_count();
var compare_text=document.replyform.message.value;
for(var i=0; i<swear_words_arr.length; i++)
{
for(var j=0; j<(compare_text.length); j++)
{
if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase())
{
swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length));
swear_alert_count++;
}
}
}
var alert_text=””;
for(var k=1; k<=swear_alert_count; k++)
{
alert_text+=”n” + “(” + k + “) ” + swear_alert_arr[k-1];
}
if(swear_alert_count>0)
{
alert(“The form cannot be submitted.nThe following illegal words were found:n_______________________________n” + alert_text + “n_______________________________”);
document.replyform.message.select();
}
else
{
document.replyform.submit();
}
}
function select_area()
{
document.replyform.message.select();
}
window.onload=reset_alert_count;
return false;
</script>
<SCRIPT>
function validateForm (form) {
for (var e = 0; e < form.elements.length; e++) {
var el = form.elements[e];
if (el.type == ‘text’ || el.type == ‘textarea’ ||
el.type == ‘password’ || el.type == ‘file’ ) {
if (el.value == ”) {
alert(‘Please enter something in the text field, ‘ + el.name);
el.focus();
return false;
}
}
}
return true;
}
</SCRIPT>
<form ONSUBMIT=”return validateForm(this)” method=”POST” action=”replyprocess.asp?boardquer=<%=boardquer%>&game=<%=game%>&console=<%=console%>&topicid=<%=topicid%>&replyid=<%=replyid%>&boardid=<%=boardid%>” name=”replyform”>
<tr><td align=”center”><font class=”text3″><b>Title</b></font>
<p><input style=”background:#9A9A9A; border: 1 solid #000000; width: 500px; font-family: Verdana, Arial; font-size: 10px; color:#FFFFFF” type=”text” name=”Title” value=”<%=title%>” maxlength=”255″></p>
</td></tr>

<tr><td align=”center”><font class=”text3″><b>Message</b></font>
<p><textarea style=”background:#9A9A9A; border: 1 solid #000000; width: 500px; height: 100px; font-family: Verdana, Arial; font-size: 10px; color:#FFFFFF” area name=”message” onclick=”select_area()””>[quote]<%=rsreply(“description”)%>[/quote]</textarea>
</p>
<tr><td align=”center”><input type=”submit” value=”Submit” cursor:pointer” value=”Submit” onclick=”validate_message();”><input type=”reset” value=”Reset” name=”B2″></td></tr>
</form>
[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@CharlesAug 13.2005 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta name="Content-Script-Type" content="text/javascript"&gt;
&lt;meta name="Content-Style-Type" content="text/css"&gt;
&lt;title&gt;Example&lt;/title&gt;

&lt;script type="text/javascript"&gt;

badWords = ['sod you', 'bollocks', 'bloody'];

function check (f) {
if (!/S/.test (f.elements.message.value)) {alert ('Please, a message is required.'); return false};

<i> </i>var word, i = 0;
<i> </i>while (word = badWords[i++]) {
<i> </i> var pattern = new RegExp ('\b' + word + '\b', 'gi');
<i> </i> f.elements.message.value = f.elements.message.value.replace (pattern, '!@#$');
<i> </i>}
}

&lt;/script&gt;

&lt;style type="text/css"&gt;
fieldset {padding:1ex; width:15em}
label {display:block; margin:1ex; text-align:right}
input, textarea {margin-left:1ex; width:10em}
button {display:block; margin:auto}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form action="some-script.pl" onsubmit="return check (this)"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Example&lt;/legend&gt;
&lt;label&gt;Title&lt;input name="title" type="text"&gt;&lt;/label&gt;
&lt;label&gt;Message&lt;textarea name="message"&gt;&lt;/textarea&gt;&lt;/label&gt;
&lt;button type="submit"&gt;Send&lt;/button&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ExuroAug 13.2005 — Don't forget to validate server-side too. All a user would have to do is disable JavaScript for a minute while they're submitting their post and they could use any of your "banned words" they want. It's always a good idea to have some JavaScript validation too though, because that way it doesn't have to go through the server [i]every single time[/i] they try to submit and something's wrong.
Copy linkTweet thisAlerts:
@XTGeminimanauthorAug 14.2005 — ok i still need a little help, i don't want it to simply edit out a word. i want it to tell the user what word's are wrong and make them try again... so i changed the script a little but i'm having a couple problems...

First off, I need to somehow reset the value of i because even if there's no badwords it still gives the alert (assuming it gave an alert in the first place)

Secondly, if possibly, I'd like to format the function to show an array of words. I tried making it into an array but to no prevail.

<i>
</i>&lt;script type="text/javascript"&gt;

badWords = ['sod you', 'bollocks', 'bloody'];

function check (f) {
if (!/S/.test (f.elements.message.value)) {alert ('Please, a message is required.'); return false};
if (!/S/.test (f.elements.title.value)) {alert ('Please, a message is required.'); return false};
var word, i = 0;
while (word = badWords[i++]) {
var pattern = new RegExp ('\b' + word + '\b', 'gi');
var alert_text="";
for(var k=1; k&lt;=swear_alert_count; k++)
{
alert_text+="n" + "(" + k + ") " + word[k-1];
}
if (i&gt;0)
{
alert("The following words are forbidden...n + alert_text + "nPlease correct your message and try again.");
document.replyform.message.select();
}

&lt;/script&gt;
Copy linkTweet thisAlerts:
@CharlesAug 14.2005 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta name="Content-Script-Type" content="text/javascript"&gt;
&lt;meta name="Content-Style-Type" content="text/css"&gt;
&lt;title&gt;Example&lt;/title&gt;

&lt;script type="text/javascript"&gt;

badWords = ['sod you', 'bollocks', 'bloody'];

function check (f) {
if (!/S/.test (f.elements.message.value)) {alert ('Please, a message is required.'); return false};

<i> </i>var count = 0, i = 0, pattern, word;
<i> </i>while (word = badWords[i++]) {
<i> </i> pattern = new RegExp ('\b' + word + '\b', 'gi');
<i> </i> while (pattern.test(f.elements.message.value)) {count++}
<i> </i>}
<i> </i>if (count) {
<i> </i> alert ('Shame on you. You have used bad words ' + count + ' times.')
<i> </i> return false;
<i> </i>}
}

&lt;/script&gt;

&lt;style type="text/css"&gt;
fieldset {padding:1ex; width:15em}
label {display:block; margin:1ex; text-align:right}
input, textarea {margin-left:1ex; width:10em}
button {display:block; margin:auto}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form action="some-script.pl" onsubmit="return check (this)"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Example&lt;/legend&gt;
&lt;label&gt;Title&lt;input name="title" type="text"&gt;&lt;/label&gt;
&lt;label&gt;Message&lt;textarea name="message"&gt;&lt;/textarea&gt;&lt;/label&gt;
&lt;button type="submit"&gt;Send&lt;/button&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
And see http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html .
Copy linkTweet thisAlerts:
@XTGeminimanauthorAug 15.2005 — is there a way for it to list the bad word along with the count tho?

for instance my message is

bloody bloody sod you

can it come out as

Shame on you for using the bad words:

bloody

bloody

sod you
×

Success!

Help @XTGeminiman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...