oTree Forum >

Instant validation on typing forms

#1 by AndreL

I have a page where subjects type 2 numerical inputs ('cash0' and 'save0'). I also parse js_vars.wealth0 from the players model.

I then use a script for instant calculation:
    let cash0Input = document.getElementsByName('icash0')[0];
    let save0Input = document.getElementsByName('isave0')[0];
    let cons0Ele = document.getElementById('cons0');

    function recalc() {
        let cash0 = parseFloat(cash0Input.value);
        let save0 = parseFloat(save0Input.value);
        cons0Ele.innerText = (js_vars.wealth0 - cash0 - save0).toFixed(2);
    cash0Input.oninput = recalc;
    save0Input.oninput = recalc;

How could I also prevent subjects from typing any value (or at least that they got an instant alert) if 'cons0' becomes negative given 'cash0' and 'save0'?

#2 by Chris_oTree

There are various ways; one is setCustomValidity:


Write a reply

Set forum username