Anti-spam system

Последнее обновление: 21 июля, 2021

security

antispam.js:

const submitButton = document.getElementsByClassName("subscribe-button")[0];
    const regex = {
        email: /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,
    };

    function isFieldValid(field, regex) 
    {
        return regex.test(String(field.value).toLowerCase());
    }



    submitButton.addEventListener("click", (e) => 
    {
        e.preventDefault();
        const emailInput = document.getElementById("userMail");
        const antispamInputs = [].slice.call(document.getElementsByName("antispam"));
        
        if(!isFieldValid(emailInput,regex.email))
        {
            emailInput.classList.add("error");
            setTimeout(() => {emailInput.classList.remove("error")},2000);
            return;
        }
        
        let status = 1;    
        
        // Check if antispam inputs are empty
        antispamInputs.forEach(element => {
            if(element.value)
            {
                status = 0;
            }
        });


        const link = "https://mededu.org.ua/wp-content/themes/metheme/telegram.php/?userMail="+ emailInput.value + "&status=" + status;
        window.location.href = link;


    });

В форму нужно вставить следующие поля:

<div style="display: none;">
    <input type="text" name="antispam">
    <input type="email" name="antispam">
    <input type="tel" name="antispam" >
    <input type="number" name="antispam">
</div>

Весь код формы:

                    <form class="subscribe-form" method="post">
                   	<input type="email"class="subscribe-input email" name="userMail" id="userMail">

                       <div style="display: none;">
                           <input type="text" name="antispam">
                           <input type="email" name="antispam">
                           <input type="tel" name="antispam" >
                           <input type="number" name="antispam">
                       </div>   
                   	<button class="button subscribe-button" type="submit"></button>
                   </form>