javascript 用于阻止注册表中垃圾邮件链接的代码

javascript code to stop spam links in registration form

我们在我们的网站上有一个会员注册表格,所有字段都通过 javascript 验证 _ 最后两次注册使用地址字段和备注字段发送了恶意软件 links _
验证码不是防止这种情况的好选择 _ 所以我查看了 javascript 中的正则表达式以禁止这些表单字段中的 'http://' 但我对如何将代码实现到原始表单验证中感到困惑_ 我想知道是否有人有一段 javascript 代码会在我们的表单中写入 'http://' _ 或者有一个 link 到我可以找到我们需要的 javascript 的地方_ 谢谢


//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;
            fNameErr.innerHTML = "You have not entered your name.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = fName;
            }
        }
        else if (!fullNameReg.test(fName.value))
        {

            fNameErr.parentElement.style.backgroundColor = bgColorErr;
            fNameErr.innerHTML = "You have not entered your name correctly.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = fName;
            }
        }
        else
        {
            fNameErr.innerHTML = "";
            fNameErr.parentElement.style.backgroundColor = bgColor;
        }


        // Date of Birth validation test:
        if (dob.value == "")
        {
            dobErr.parentElement.style.backgroundColor = bgColorErr;
            dobErr.innerHTML = "You have not entered your date of birth.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = dob;
            }
        }
        else if (!dobReg.test(dob.value))
        {
            dobErr.parentElement.style.backgroundColor = bgColorErr;
            dobErr.innerHTML = "You have not entered your date of birth correctly.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = dob;
            }
        }
        else
        {
            dobErr.innerHTML = "";
            dobErr.parentElement.style.backgroundColor = bgColor;
        }


        // Address validation test:
        if (address.value == "")
        {
            addressErr.parentElement.style.backgroundColor = bgColorErr;
            addressErr.innerHTML = "You have not entered your address.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = address;
            }
        }
        else if (address.value.length < 5)
        {
            addressErr.parentElement.style.backgroundColor = bgColorErr;
            addressErr.innerHTML = "You have not entered your address correctly.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = address;
            }
        }
        else
        {
            addressErr.innerHTML = "";
            addressErr.parentElement.style.backgroundColor = bgColor;
        }


        // Mobile Number validation test:
        if (telMob.value == "")
        {
            telMobErr.parentElement.style.backgroundColor = bgColorErr;
            telMobErr.innerHTML = "You have not entered your mobile phone number.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = telMob;
            }
        }
        else if (telMob.value.length < 11 || !numberReg.test(telMob.value))
        {
            telMobErr.parentElement.style.backgroundColor = bgColorErr;
            telMobErr.innerHTML = "You have not entered your mobile phone number correctly.<br /><br />";
            formValid = false;
            if (focusField === null)
            {
                focusField = telMob;
            }
        }
        else
        {
            telMobErr.innerHTML = "";
            telMobErr.parentElement.style.backgroundColor = bgColor;
        }


        // Home Number validation test:
        if (telHome.value != "" && (telHome.value.length < 9 || !numberReg.test(telHome.value)))
        {
            telHomeErr.parentElement.style.backgroundColor = bgColorErr;
            telHomeErr.innerHTML = "You have not entered your home phone number correctly.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = telHome;
            }
        }
        else
        {
            telHomeErr.innerHTML = "";
            telHomeErr.parentElement.style.backgroundColor = bgColor;
        }

        // Email Address validation test:
        if (email.value == "")
        {
            emailErr.parentElement.style.backgroundColor = bgColorErr;
            emailErr.innerHTML = "You have not entered your email address.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = email;
            }
        }
        else if (!emailReg.test(email.value))
        {
            emailErr.parentElement.style.backgroundColor = bgColorErr;
            emailErr.innerHTML = "You have not entered your email address correctly.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = email;
            }
        }
        else
        {
            emailErr.innerHTML = "";
            emailErr.parentElement.style.backgroundColor = bgColor;
        }


        // Game Console Type validation test:
        if (consoleType.value == "")
        {
            consoleTypeErr.parentElement.style.backgroundColor = bgColorErr;
            consoleTypeErr.innerHTML = "You have not selected your console type.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = consoleType;
            }
        }
        else
        {
            consoleTypeErr.innerHTML = "";
            consoleTypeErr.parentElement.style.backgroundColor = bgColor;
        }


        // Customer Wishlist validation test:
        if (wishList.value == "")
        {
            wishListErr.parentElement.style.backgroundColor = bgColorErr;
            wishListErr.innerHTML = "You have not entered any games in your Wishlist.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = wishList;
            }
        }
        else if (wishList.value.length < 3)
        {
            wishListErr.parentElement.style.backgroundColor = bgColorErr;
            wishListErr.innerHTML = "You have not entered at least one correct game in your Wishlist.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = wishList;
            }
        }
        else
        {
            wishListErr.innerHTML = "";
            wishListErr.parentElement.style.backgroundColor = bgColor;
        }


        // Customer Gamer Tag or Name validation test:
        if (gamerTag.value == "")
        {
            gamerTagErr.parentElement.style.backgroundColor = bgColorErr;
            gamerTagErr.innerHTML = "You have not entered an online ID or your name.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = gamerTag;
            }
        }
        else
        {
            gamerTagErr.innerHTML = "";
            gamerTagErr.parentElement.style.backgroundColor = bgColor;
        }


        // Terms and Conditions acceptance test:
        if (!tAndC.checked)
        {
            tAndCErr.parentElement.style.backgroundColor = bgColorErr;
            tAndCErr.innerHTML = "You have not ticket to confirm reading the terms and conditions.<br /><br />";
            formValid = false;

            if (focusField === null)
            {
                focusField = tAndC;
            }
        }
        else
        {
            tAndCErr.innerHTML = "";
            tAndCErr.parentElement.style.backgroundColor = bgColor;
        }


        // Focus on the first problem field.
        if (focusField != null)
        {
            focusField.focus();
        }

        // Return the validity of the form.     
        return formValid;       
    }

});

我认为 honeypot technique 是一个非常优雅、简单的反垃圾邮件设备 - 只需用 css 隐藏一个字段,如果它被填充,你就知道这是一个垃圾邮件机器人在做,所以垃圾邮件投稿

正则表达式应如下所示:

var inputText = "my adreess is city ... and spam http://somelink... and another www.link... 50";

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

document.write( 
  
  inputText.replace(regexClearSites, '')

);

解释正则表达式:

(?:http|www)       # any text starting with "http" or "www"
\S+\s*             # till the next space

希望对您有所帮助。