Jump to content

form validation - no swear words in feedback field?


foevah

Recommended Posts

I have this real simple form that I want to add validation checking to. So if the user enters in some swear words in the feedback and then they submit the form I wont it to say underneath the form HEY YOU ARE NOT ALLOWED TO SWEAR! I would also like to check if the user has entered a correct email using the @ sign. I am not sure how to do this. Please can someone help?

<form method="post" action="processfeedback.php">
Your name: <br />
<input type=text name="name" size=40><br />
Your email address: <br />
<input type=text name="email" size=40><br />
Your feedback:<br />
<textarea name="feedback" rows=5 cols=30>
</textarea><br />
<input type="submit" value="Send feedback">
</form>

Link to comment
Share on other sites

add this script above your mail() function.

 

<?php

$badwords=array("badword","swearword"); // add each prohibited word in this array

// repeat the IF condition below for each field you want to be filtered

if (eregi('('.implode('|',$badwords).')', $feedback){
echo "HEY YOU ARE NOT ALLOWED TO SWEAR!";
exit;
}

// email address validation to check for specific symbols

if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
echo "PLEASE ENTER A VALID EMAIL ADDRESS!";
exit;
}

?>

Link to comment
Share on other sites

I added your code above the mail() function and the browser gives me this message:

Parse error: parse error, unexpected '{' in /home/webmedia/public_html/jecgardner/phpexamples/reg_exp/processfeedback.php on line 18

 

This is the processfeedback.php with your code added:

<?php
  //create short variable names
  $name=$HTTP_POST_VARS['name'];
  $email=$HTTP_POST_VARS['email'];
  $feedback=$HTTP_POST_VARS['feedback'];

  $toaddress = 'feedback@example.com';
  $subject = 'Feedback from web site';
  $mailcontent = 'Customer name: '.$name."\n"
                 .'Customer email: '.$email."\n"
                 ."Customer comments: \n".$feedback."\n";
  $fromaddress = 'From: webserver@example.com';

$badwords = array("fuck","swearword"); // add each prohibited word in this array

// repeat the IF condition below for each field you want to be filtered

if (eregi('('.implode('|',$badwords).')', $feedback){
echo "HEY YOU ARE NOT ALLOWED TO SWEAR!";
exit;
}

// email address validation to check for specific symbols

if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
echo "PLEASE ENTER A VALID EMAIL ADDRESS!";
exit;
}

  mail($toaddress, $subject, $mailcontent, $fromaddress);
?>
<html>
<head>
  <title>Feedback Submitted</title>
</head>
<body>
<h1>Feedback submitted</h1>
<p>Your feedback has been sent.</p>
</body>
</html>

 

In this tutorial I am reading it says

Note that generally you should check users have filled out all the required form fields using, for example isset(). We have omitted this from the script and other examples for sake of brevity.

 

I have tried finding a tutorial on adding the isset() function to this feedback form but I can't find anything that will  help me. Can someone explain how I can add isset() to this form please?

 

I would also like to know why phpQuestioner's swear word filter and email validation doesnt work?

Link to comment
Share on other sites

Try this.

$badwords = array(
'shit','fuck'
);

if(in_array($feedback,$array)){
  print "You cant swear";
  exit;
}

 

Also i would replace:

if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))

 

with:

if(!preg_match('|^[a-z0-9_.%-]+@[a-z0-9_-]+\.[a-z0-9]{2,4}$|i', $email))

 

1. preg_match is must faster than ereg.

2. ^ $ removes all symbols (foreign characters)

3. |i makes it case-insensitive.

4. i fixed a few things you were missing.

 

 

Regards.

Link to comment
Share on other sites

I replaced phpQuestioner's code with yours xyn and now the browser says:

Warning: in_array(): Wrong datatype for second argument in /home/webmedia/public_html/jecgardner/phpexamples/reg_exp/processfeedback.php on line 18

PLEASE ENTER A VALID EMAIL ADDRESS!

 

Can someone explain the isset() function that this tutorial im reading decides to miss out?

Note that generally you should check users have filled out all the required form fields using, for example isset(). We have omitted this from the script and other examples for sake of brevity.

 

Also is it possible for it to say you can't swear on the same page as the form instead of going to a new page then having to click back? So if the user enters an invalid email and swears in the feedback text area can it say invalid email and you can't swear underneath each field instead of it going to a new page??

 

I have tried using this swear word code but the browser gives me another error message:

$swarewords= array(bad,mouth,mother,screw,off);

if ($swarewords=!) { echo "no sware words were found."; } else { echo "You naughy person you!"; }

Parse error: parse error, unexpected ')' in /home/webmedia/public_html/jecgardner/phpexamples/reg_exp/processfeedback.php on line 16

Link to comment
Share on other sites

http://us.php.net/isset

 

Basically checks to see if something is set or not.

 

 

Can someone explain the isset() function that this tutorial im reading decides to miss out?

Note that generally you should check users have filled out all the required form fields using, for example isset(). We have omitted this from the script and other examples for sake of brevity.

 

Link to comment
Share on other sites

You could use javascript for that if you wished. Something like:

 

 

The form:

<form action="feedback.php" method="post" onsubmit="return checkForm();">
<div id="d_feedback" style="display: none;">Please do not swear.</div><br />
Subject:<input type="text" name="subject" /><br />
Content:<textarea name="content" id="content" cols="10" rows="5"></textarea>

 

 

The script:

<script type="text/javascript>
function checkForm(form)
{
 content = document.getElementById('d_content');
 if(form.content.indexOf('fuck') >- 1)
 {
   content.style.display = 'block';
   form.content.focus;
   return false;
 }
 else
 {
   content.style.display = 'none';
 }
 return true;
}

 

that should work i think :S

Link to comment
Share on other sites

try this

 

 

<?php

function check_email_simple(&$email) {

if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$',$email)) {
return("ERROR");
} else {
return("SAFE");
}

}

function check_badwords(&$feedback) {

#make all feedback lower case
$feedback = strtolower($feedback);

$badwords = array (1=>
'badword1','badword2','badword3');

$total_array= count($badwords);

for ($x = 1; $x <= $total_array; $x++) {

if (strstr($feedback,$badwords($x))) {
return("ERROR");

}

}
return("SAFE");

}

$feedback=mysql_real_escape_string($_POST['feedback'];
$name =mysql_real_escape_string($_POST['name'];
$email = mysql_real_escape_string($_POST['email'];

if (check_email_simple($email) == "ERROR") {

echo "your email is wrong";
exit();
}

if (check_badwords($feedback) == "ERROR") {

echo "you can't use that word in our site";
exit();
}


echo "its safe mate";


?>

 

 

Sorry for double post

Link to comment
Share on other sites

<?php

function check_email_simple(&$email) {

if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$',$email)) {
return("ERROR");
} else {
return("SAFE");
}

}

function check_badwords(&$feedback) {

#make all feedback lower case
$feedback = strtolower($feedback);

$badwords = array (1=>
'badword1','badword2','badword3');

$total_array= count($badwords);

for ($x = 1; $x <= $total_array; $x++) {

if (strstr($feedback,$badwords($x))) {
return("ERROR");

}

}
return("SAFE");

}

$feedback=addslashes($_POST['feedback']);
$name =addslashes($_POST['name']);
$email =addslashes($_POST['email']);

if (check_email_simple($email) == "ERROR") {

echo "your email is wrong";
exit();
}

if (check_badwords($feedback) == "ERROR") {

echo "you can't use that word in our site";
exit();
}


echo "its safe mate";


?>

 

 

Lol this doesn't work either some unidentified header error I get. Its too late for me to debug it really have fun :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.