[jQuery] Bot-Filter - Minimum Submission

BotFilter v1.3 | Misoss - 2019 

 

 

test-page.html

<form>
        First name:<br><input type="text" id="tbxFirstname" name="firstname"><br>
        Last name:<br><input type="text" id="tbxLastname" name="lastname"><br><br>
        <input type="submit" id="btnSubmit">
</form>
 
<script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function () {
        var controllerNeedCheck = "#tbxFirstname, #tbxLastname"
        var controllerChanged = "#btnSubmit"
        BotFilter(controllerNeedCheck, controllerChanged);
    });
</script>

 

 

botfilter.js

/*! botFilter v1.3 | Misoss - 2018 */
// Outils pour bloquer l'accès aux formulaires aux bots (Anti-Spam)
/*! Params ////////////////////// */
var botFilter_TimerLimit = 2500; //(Millisecond)
var botFilter_TimerSpeedLoop = 100; //(Millisecond)
/*! .Params ////////////////////// */
 
function BotFilter(elListend, elNeedChanged) {
    startUI(elListend, elNeedChanged)
    isEnabledByOtherElement(elListend, elNeedChanged);
    checkTouch(elNeedChanged);};
 
function startUI(elListend, elNeedChanged) {
    $(elListend).prop('required', false);
    $(elNeedChanged).attr('class', 'botfilter-btn');
    $(elNeedChanged).attr('title', 'Bot-Filter ! Protection anti-robot, merci de rester appuyé 2secondes sur le bouton.');
    buttonUI(elNeedChanged, false);};
 
function isEnabledByOtherElement(elListend, elNeedChanged) {
    $(elListend).on('keyup', function () { buttonUI(elNeedChanged, !checkStatementElementListend(elListend)); });
};
 
function checkStatementElementListend(elListend) {
    var elHaveStatementFalse = false;
    var elItem = elListend.toString().split(",");
    var elLoopCurrent = '';
    for (i = 0; i < elItem.length; i++) {
        elLoopCurrent = elItem[i].toString().trim();
        console.log(elLoopCurrent + ' : ' + $(elLoopCurrent).val())
        if (($(elLoopCurrent).length != 0) && ($(elLoopCurrent).val() == '')) { elHaveStatementFalse = true; };}
    return elHaveStatementFalse;
};
 
function buttonUI(elNeedChanged, statement) {
    $(elNeedChanged).attr('disabled', !statement);
    if (statement) {
        $(elNeedChanged).attr('class', 'botfilter-btn open');
    }
    else { $(elNeedChanged).attr('class', 'botfilter-btn close'); }};
 
function checkTouch(elNeedChanged) {
    var TimerInfos = (botFilter_TimerLimit / 1000);
    var TimerLoop = 0;
    var TimerId;
    $(elNeedChanged).mousedown(function (event) {
        event.preventDefault();
        event.stopPropagation();
        TimerId = setInterval(function () {
            TimerLoop += botFilter_TimerSpeedLoop;
            var customTxt;
 
            if (TimerInfos >= 0) { TimerInfos = ((botFilter_TimerLimit - TimerLoop) / 1000); customTxt = TimerInfos.toFixed(0) + 's'; }
            else { TimerInfos <= 0; clearInterval(TimerId); customTxt = 'Envoi'; }
            $(elNeedChanged).prop('value', customTxt);
        }, botFilter_TimerSpeedLoop);
    });
 
    $(elNeedChanged).mouseup(function (event) {
        if (TimerLoop >= botFilter_TimerLimit) {
            //return confirm('Do you really want to submit the form?');
            $(elNeedChanged).prop('value', 'Envoyé !');
            return true;
        }
        else {
            clearInterval(TimerId);
            TimerLoop = 0;
            $(elNeedChanged).prop('value', 'Recommencer');
            event.preventDefault();
            event.stopPropagation();
            return false;}});};