Preventing spam links on a registration form using JavaScript

Our website has a membership registration form with JavaScript validation. Unfortunately, the last two registrations have included malware links in the address and notes fields. Using Captcha is not an effective solution to prevent this issue. I've been exploring regular expressions in JavaScript to ban entries containing 'http://' in these form fields, but I'm having trouble implementing the code into the existing form validation logic.
If anyone has a snippet of JavaScript code that can detect and invalidate 'http://' entries in our form, or if you know where I can find such code, please share it. Thank you.


//Define global variables:
var fullNameReg = /^([a-zA-Z]+(-|\s*)[a-zA-Z]*)+$/;
var numberReg = /^([0-9]+(\s*|-)*[0-9]*)+$/;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var dobReg = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/;

var bgColorErr = "#FFC36E";
var bgColor = "#FFE4BD";


$(document).ready(function(){

    document.getElementById("registrationForm").onsubmit = function onSubmit(form)
    {
        // Define logic variables:
        var formValid = true;
        var focusField = null;


        // Define form elememt variables:
        var fName = document.getElementById("fName");
        var dob = document.getElementById("dob");
        var address = document.getElementById("address");
        var telMob = document.getElementById("telMob");
        var telHome = document.getElementById("telHome");
        var email = document.getElementById("email");
        var consoleType = document.getElementById("consoleType");
        var wishList = document.getElementById("wishList");
        var gamerTag = document.getElementById("gamerTag");
        var tAndC = document.getElementById("tAndC");

        // Define form error span variables:
        var fNameErr = document.getElementById("fNameErr");
        var dobErr = document.getElementById("dobErr");
        var addressErr = document.getElementById("addressErr");
        var telMobErr = document.getElementById("telMobErr");
        var telHomeErr = document.getElementById("telHomeErr");
        var emailErr = document.getElementById("emailErr");
        var consoleTypeErr = document.getElementById("consoleTypeErr");
        var wishListErr = document.getElementById("wishListErr");
        var gamerTagErr = document.getElementById("gamerTagErr");
        var tAndCErr = document.getElementById("tAndCErr");


        // Full Name validation test:
        if (fName.value == "")
        {
            fNameErr.parentElement.style.backgroundColor = bgColorErr;
            ...

// The rest of the original code remains unchanged for brevity.

Answer №1

This is the recommended regular expression format:

let text = "my address is city ... and spam http://somelink... and another www.link... 50";

let clearSitesRegex = /(?:http|www)\S+\s*/g;

document.write( 
  
  text.replace(clearSitesRegex, '')

);

Breaking down the regex pattern:

(?:http|www)       # any string beginning with "http" or "www"
\S+\s*             # up to the next space character

Hopefully this explanation clarifies things.

Answer №2

In my opinion, the cleverly devised honeypot technique serves as an effective and uncomplicated tool to combat spam - by concealing a field with CSS, any input into it indicates the presence of a spambot, prompting the rejection of the submission.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Place the script tags within the window.load function inside the head section

<head> <script type="text/javascript"> $(window).load(function() { // <script src="js/external.js"></script> // }); </script> </head> Is it possible to insert a script tag(< script src="js/exte ...

Error Timeout Encountered by Langchain UnstructuredDirectoryLoader

I am facing an issue while trying to load a complex PDF file with tables and figures, spanning approximately 600 pages. When utilizing the fast option in Langchain-JS with NextJS Unstructured API, it partially works but misses out on some crucial data. On ...

What could be causing the jQuery spritely animation to display an additional frame upon the second mouseenter event?

I have been experimenting with CSS sprites and the jQuery plugin called spritely. My goal is to create a rollover animation using a Super Mario image. When the mouse hovers over the Super Mario <div>, I want the animation to play forward. And when t ...

Is it possible to use null and Infinity interchangeably in JavaScript?

I've declared a default options object with a max set to Infinity: let RANGE_DEFAULT_OPTIONS: any = { min: 0, max: Infinity }; console.log(RANGE_DEFAULT_OPTIONS); // {min: 0, max: null} Surprisingly, when the RANGE_DEFAULT_OPTIONS object is logged, i ...

What is the method for determining the dimensions of the rectangle that the camera can capture at a specific location?

In my latest project, I developed a small three.js application that showcases the movement of multiple circles from the bottom to the top of the canvas: let renderer, scene, light, circles, camera; initialize(); animate(); function initialize() { re ...

The getJSON function callback is not functioning as expected

I'm new to working with JSON objects and sending them to the browser. I'm having trouble with the callback function not executing properly. I don't see anything in the console. $('#id').change(function(){ $.getJSON('ajax. ...

Strategies for making a child div fade out when the parent div is hovered over

I have a div with the class name ordershape and inside it, there is another div called fad-res. My goal is to display the corresponding fad-res when I hover over a specific ordershape, while hiding the other divs. <div class="ordershape"> & ...

Identifying an Incorrect Function Call in a TypeScript Function from a JavaScript File [TypeScript, Vue.js, JavaScript]

I have a vue2 application and I am looking to incorporate TypeScript into some service files without modifying the existing js/vue files. To enable TypeScript support, I utilized vue-cli which allowed me to successfully add a myService.ts file containing ...

Update the image source every 20 pixels

I am trying to adjust the image source while scrolling down by 20 pixels, so that after 100 pixels the image source changes 5 times, with the initial src image being "falling-05.png". Despite following various tutorials, the implementation doesn't see ...

Live formatting of phone numbers using regular expressions

For my AngularJS project, I am looking to format phone numbers as they are being typed, without relying on any external library. The desired format is: 99 99 99 99 99 var phone = tel.replace(/\D*(\d{2})\D*(\d{2})\D*(\d{2})&b ...

Need help resolving the issue of retrieving feed error in Angular?

I am encountering an issue in Chrome that displays an error message: Error fetching feed: Undefined, 0. Do you have any suggestions on how to resolve this? Below is the Angular code I am using: // Implementing SimpleController, with data demoApp.controll ...

Express functions properly when handling the root route, but encounters issues with sub routes as it sends empty response bodies

Inside the routes.ts file: const router:Router = express.Router() // Route to get all blogs router.get('/',async (req:Request,res:Response)=>{ res.status(200).send("message sent") }) router.get('/admin',async (req:Requ ...

I need to retrieve data from two different databases. What is the best way to combine the code into a single function?

I am working on fetching data from 2 database sources and combining them to send the merged data to result.html function showResult(req, res){ var n = req.query.query mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE ...

AngularJS and TypeScript encountered an error when trying to create a module because of a service issue

I offer a service: module app { export interface IOtherService { doAnotherThing(): string; } export class OtherService implements IOtherService { doAnotherThing() { return "hello."; }; } angular.mo ...

What could be preventing this bootstrap carousel slider from transitioning smoothly?

I've created a CodePen with what I thought was a functional slider, but for some reason, the JavaScript isn't working. The setup includes bootstrap.css, jquery.js, and bootstrap.js. I'm having trouble pinpointing what's missing that&apo ...

Enhance the php query limit using javascript when loading additional content

Hey there, currently working on implementing a reverse infinite scroll feature for a private messaging module. Everything seems to be functioning well, except for one issue I'm facing - struggling to update the query limit. I set the maximum and lim ...

The input given to Material UI autocomplete is incorrect, even though the getOptionSelect parameter already exists

I'm currently working on creating my own version of the Google Places autocomplete found in the Material UI documentation. While I have successfully implemented the autocomplete feature and am able to update my component's state with the result, ...

Allowing unauthorized cross-origin requests to reach the SailsJS controller despite implementing CORS restrictions

My cors.js file has the following configuration: module.exports.cors = { allRoutes: true, origin: require('./local.js').hosts, // which is 'http://localhost' } I decided to test it by making a request from a random site, Here is ...

Utilize the fetch function to showcase information retrieved from a specific endpoint on a webpage using Javascript

Hey there, I have a Node server with an http://localhost:3000/community endpoint. When I make a GET request to this endpoint, it returns information about three different users in the form of JSON objects. [ { "avatar": "http://localhost:3000/avatars ...

The GridFS/multer module is encountering an issue where it is unable to access the 'filename' property of an undefined

My knowledge in web development is limited, so forgive me if this question seems naive. The issue I am facing involves Node.Js and the creation of a database to store and display images on a browser using an .ejs file. While I can successfully log the im ...