How to Use Regular Expressions to Block Spam
Use a JavaScript regular expression function to apply an approach to blocking spam email that uses keywords. JavaScript's "RegExp" class lets you create a virtual object whose "Test" function can search for spam keywords in text from an HTML form. That function returns "true" if any word in the form's content matches a spam keyword, and returns "false" when the content matches no keywords. Your Web form will still forward spam to your server that you don't catch with a keyword. However, a carefully crafted keyword list will prevent much spam from reaching the server.
Instructions
-
-
1
Open WordPad or another text editor, then paste into its application window the HTML statements that follow:
<html>
<head>
</head>
<body>
<form name="mailForm" action="" onsubmit="return doSpamTest()" method="post">
Email message: <textarea type="text" name="email">Type your message here.</textarea>
<input type="submit" value="Submit message ">
</form>
</body>
</html>These statements define a Web page with a form for sending email. The form control that will hold a message that the page's user types is a "Textarea." The Textarea in this example has its "Name" attribute set to the value "email," which allows a JavaScript program to easily find the Textarea.
-
2
Click the mouse one line down from the "Head" tag, then paste the following JavaScript "EXCERPT" into the application window:
var spamText = ["wordA", "wordB", "wordC"];
This EXCERPT defines a variable called "form_data" that holds the email message typed into the Textarea named "email." The "spamText" variable is an array of strings that represent spam keywords.
-
-
3
Type over the "wordA," "wordB" and "wordC" keywords with expletives or other words you want your Web page to look for to identify spam.
-
4
Paste the following JavaScript statement after the spamText statement:
var rex = new RegExp(spamText.join("|"));
This statement calls the "RegExp" constructor function to create a new regular expression object. The constructor function takes a single argument, a regular expression pattern to match against text. The "Join" function used in the argument merges all spam keywords into a single text item, separating the keywords with a "|" symbol. The JavaScript parser in your Web browser interprets this symbol as an "OR" condition, which means it reads the sample pattern "word one|word two" as "search for a match on word one OR word two."
-
5
Paste the following "If" clause after the RegExp statement:
if(rex.test(form_data)){
alert ("Spam!");
return false;
}else{
alert ("Not spam");
return true;
}This clause displays message boxes that show you if the regular expression Test function matched a spam keyword or not.
-
6
Click the "File" menu's "Save" command, then type a file name in the "File name" text box of the dialog box that appears. Click the "Any" item from the "Type" drop-down list to indicate this file is a text file without the ".txt" extension. Click "Save" to save the Web page to disk.
-
7
Open Windows Explorer, then navigate to and double-click the Web page you just saved to open it in your browser.
-
8
Type a sample email message in the page's text area. Include one of the spam keywords in the message. Click the page's button to run your JavaScript program, which will display the "Spam" message.
-
1