#How to make that button will be disabled until all inputs are filled?

77 messages · Page 1 of 1 (latest)

unkempt cave
#

check for the conditions and create a function to handle the case (perhaps add a class to the button and toggle the class to change style and remove the listener if conditions are matched)

stuck gust
#

im more looking for source code

#

🥺

#

since mine dont work

#
 document.addEventListener("DOMContentLoaded", function () {
        const form = document.getElementById("registrationForm");
        const usernameInput = document.getElementById("username");
        const emailInput = document.getElementById("email");
        const passwordInput = document.getElementById("password");
        const button = document.getElementById("registerconfirm");
    
        // Create a function to continuously check and update the button's state
        function updateButtonState() {
          const isUsernameValid = usernameInput.value.trim() !== "";
          const isEmailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput.value);
          const isPasswordValid = passwordInput.value.length >= 6;
    
          if (isUsernameValid && isEmailValid && isPasswordValid) {
            button.removeAttribute("disabled");
            button.style.cursor = "pointer";
          } else {
            button.setAttribute("disabled", "disabled");
            button.style.cursor = "not-allowed";
          }
        }
patent mist
stuck gust
#

the button keep being disabled even tho i fill everything right

orchid portal
#

You must define updateButtonState() outside of the event handler and create another event listener which runs this code when the relevant input values are changed.

stuck gust
# orchid portal You must define updateButtonState() outside of the event handler and create anot...

like this?


function updateButtonState() {
    const isUsernameValid = usernameInput.value.trim() !== "";
    const isEmailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput.value);
    const isPasswordValid = passwordInput.value.length >= 6;

    if (isUsernameValid && isEmailValid && isPasswordValid) {
        button.removeAttribute("disabled");
        button.style.cursor = "pointer";
    } else {
        button.setAttribute("disabled", "disabled");
        button.style.cursor = "not-allowed";
    }
}

document.addEventListener("DOMContentLoaded", function () {
    const form = document.getElementById("registrationForm");
    const usernameInput = document.getElementById("username");
    const emailInput = document.getElementById("email");
    const passwordInput = document.getElementById("password");
    const button = document.getElementById("registerconfirm");

    
    updateButtonState();

   
    usernameInput.addEventListener("input", updateButtonState);
    emailInput.addEventListener("input", updateButtonState);
    passwordInput.addEventListener("input", updateButtonState);
});

#

cuz its still not working -_-

unkempt cave
orchid portal
stuck gust
# orchid portal This code still has a problem. You run getElementById() too soon. Run it inside ...
function updateButtonState() {
        const isUsernameValid = usernameInput?.value.trim() !== "";
        const isEmailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput?.value);
        const isPasswordValid = passwordInput?.value.length >= 6;

            const form = document.getElementById("registrationForm");
            const usernameInput = document.getElementById("username");
            const emailInput = document.getElementById("email");
            const passwordInput = document.getElementById("password");
            const button = document.getElementById("registerconfirm");
        if (isUsernameValid && isEmailValid && isPasswordValid) {
            button.removeAttribute("disabled");
            button.style.cursor = "pointer";
        } else {
            button.setAttribute("disabled", "disabled");
            button.style.cursor = "not-allowed";
        }
    }
    
    
    
        // Attach input event listeners to trigger the function when input values change
        usernameInput.addEventListener("input", updateButtonState);
        emailInput.addEventListener("input", updateButtonState);
        passwordInput.addEventListener("input", updateButtonState);
    
        // Initial call to updateButtonState when the page is fully loaded
        window.addEventListener("load", updateButtonState);

    ```
like that? 😭
orchid portal
orchid portal
stuck gust
#
const form = document.getElementById("registrationForm");
    const usernameInput = document.getElementById("username");
    const emailInput = document.getElementById("email");
    const passwordInput = document.getElementById("password");
    const button = document.getElementById("registerconfirm");
    function updateButtonState() {
        
        const isUsernameValid = usernameInput?.value.trim() !== "";
        const isEmailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput?.value);
        const isPasswordValid = passwordInput?.value.length >= 6;

            
        if (isUsernameValid && isEmailValid && isPasswordValid) {
            button.removeAttribute("disabled");
            button.style.cursor = "pointer";
        } else {
            button.setAttribute("disabled", "disabled");
            button.style.cursor = "not-allowed";
        }
    }
    
    
    
        // Attach input event listeners to trigger the function when input values change
        usernameInput.addEventListener("input", updateButtonState);
        emailInput.addEventListener("input", updateButtonState);
        passwordInput.addEventListener("input", updateButtonState);
    
        // Initial call to updateButtonState when the page is fully loaded
        window.addEventListener("load", updateButtonState);
#

putted it higher

orchid portal
# stuck gust how im supposed to do it

Remove the last line of code. The other event listeners are calling the function at the right time. That last one calls it when the page loads. That is before the user has had time to do anything.

stuck gust
#
    const form = document.getElementById("registrationForm");
const usernameInput = document.getElementById("username");
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");
const button = document.getElementById("registerconfirm");

function updateButtonState() {
    const isUsernameValid = usernameInput?.value.trim() !== "";
    const isEmailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput?.value);
    const isPasswordValid = passwordInput?.value.length >= 6;

    if (isUsernameValid && isEmailValid && isPasswordValid) {
        button.removeAttribute("disabled");
        button.style.cursor = "pointer";
    } else {
        button.setAttribute("disabled", "disabled");
        button.style.cursor = "not-allowed";
    }
}

// Attach input event listeners to trigger the function when input values change
usernameInput.addEventListener("input", updateButtonState);
emailInput.addEventListener("input", updateButtonState);
passwordInput.addEventListener("input", updateButtonState);


    
#

removed, still not working

orchid portal
stuck gust
#
function updateButtonState() {
const form = document.getElementById("registrationForm");
const usernameInput = document.getElementById("username");
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");
const button = document.getElementById("registerconfirm");
    const isUsernameValid = usernameInput?.value.trim() !== "";
    const isEmailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput?.value);
    const isPasswordValid = passwordInput?.value.length >= 6;

    if (isUsernameValid && isEmailValid && isPasswordValid) {
        button.removeAttribute("disabled");
        button.style.cursor = "pointer";
    } else {
        button.setAttribute("disabled", "disabled");
        button.style.cursor = "not-allowed";
    }
}

// Attach input event listeners to trigger the function when input values change
usernameInput.addEventListener("input", updateButtonState);
emailInput.addEventListener("input", updateButtonState);
passwordInput.addEventListener("input", updateButtonState);
#

still not working 👍

orchid portal
stuck gust
#
function updateButtonState() {
const form = document.getElementById("registrationForm");
const usernameInput = document.getElementById("username");
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");
const button = document.getElementById("registerconfirm");
    const isUsernameValid = usernameInput?.value.trim() !== "";
    const isEmailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput?.value);
    const isPasswordValid = passwordInput?.value.length >= 6;

console.log(usernameInput)

    if (isUsernameValid && isEmailValid && isPasswordValid) {
        button.removeAttribute("disabled");
        button.style.cursor = "pointer";
    } else {
        button.setAttribute("disabled", "disabled");
        button.style.cursor = "not-allowed";
    }
}

// Attach input event listeners to trigger the function when input values change
usernameInput.addEventListener("input", updateButtonState);
emailInput.addEventListener("input", updateButtonState);
passwordInput.addEventListener("input", updateButtonState);


orchid portal
stuck gust
#

done

orchid portal
#

I just realized that there is another issue. You were right to put those getElementById() statements before the function. That is needed to get the event listeners to work. Move them back above the function definition.

stuck gust
orchid portal
stuck gust
#
function updateButtonState() {
    const form = document.getElementById("registrationForm");
    const usernameInput = document.getElementById("username");
    const emailInput = document.getElementById("email");
    const passwordInput = document.getElementById("password");
    const button = document.getElementById("registerconfirm");

    const isUsernameValid = !!usernameInput.value && usernameInput.value.trim() !== "";
    const isEmailValid = !!emailInput.value && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput.value);
    const isPasswordValid = !!passwordInput.value && passwordInput.value.length >= 6;

    console.log("Username valid:", isUsernameValid);
    console.log("Email valid:", isEmailValid);
    console.log("Password valid:", isPasswordValid);

    if (isUsernameValid && isEmailValid && isPasswordValid) {
        button.removeAttribute("disabled");
        button.style cursor = "pointer";
    } else {
        button.setAttribute("disabled", "disabled");
        button.style.cursor = "not-allowed";
    }
}

// Attach input event listeners to trigger the function when input values change
const usernameInput = document.getElementById("username");
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");

usernameInput.addEventListener("input", updateButtonState);
emailInput.addEventListener("input", updateButtonState);
passwordInput.addEventListener("input", updateButtonState);
orchid portal
stuck gust
#
function updateButtonState() {
    const form = document.getElementById("registrationForm");
    const usernameInput = document.getElementById("username");
    const emailInput = document.getElementById("email");
    const passwordInput = document.getElementById("password");
    const button = document.getElementById("registerconfirm");

    const isUsernameValid = !!usernameInput.value && usernameInput.value.trim() !== "";
    const isEmailValid = !!emailInput.value && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput.value);
    const isPasswordValid = !!passwordInput.value && passwordInput.value.length >= 6;

    if (isUsernameValid && isEmailValid && isPasswordValid) {
        console.log("Username valid:", isUsernameValid);
        console.log("Email valid:", isEmailValid);
        console.log("Password valid:", isPasswordValid);

        button.removeAttribute("disabled");
        button.style.cursor = "pointer";
    } else {
        button.setAttribute("disabled", "disabled");
        button.style.cursor = "not-allowed";
    }
}

// Attach input event listeners to trigger the function when input values change
const usernameInput = document.getElementById("username");
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");

usernameInput.addEventListener("input", updateButtonState);
emailInput.addEventListener("input", updateButtonState);
passwordInput.addEventListener("input", updateButtonState);

#

oh yea

#

sec

#

nvm its there lol

orchid portal
# stuck gust

I think you missed my comment twice now. Move the getElementById() code before the function. I was mistaken when I previously said move it into the function.

stuck gust
#

💀

orchid portal
#

Disregard that last advice. I see that you redefined outside the function. That also works, but is less efficient.

stuck gust
#

ight lemme keep them in function i guess

orchid portal
# stuck gust

That looks like you have redefinition of constants.

orchid portal
stuck gust
#

if i put then in function it will redifition but only for checking inputs?

orchid portal
stuck gust
#

nvm it makes no sense

#

sec

orchid portal
# stuck gust

It looks like you are defining the same constants before and after the function. Delete the ones after. Lines 320-322.

stuck gust
#

oh yea

#

so i put it in function or before

orchid portal
stuck gust
#

ight

  const form = document.getElementById("registrationForm");
        const usernameInput = document.getElementById("username");
        const emailInput = document.getElementById("email");
        const passwordInput = document.getElementById("password");
        const button = document.getElementById("registerconfirm");
    function updateButtonState() {
        

        const isUsernameValid = !!usernameInput.value && usernameInput.value.trim() !== "";
        const isEmailValid = !!emailInput.value && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput.value);
        const isPasswordValid = !!passwordInput.value && passwordInput.value.length >= 6;
    
        if (isUsernameValid && isEmailValid && isPasswordValid) {
            console.log("Username valid:", isUsernameValid);
            console.log("Email valid:", isEmailValid);
            console.log("Password valid:", isPasswordValid);
    
            button.removeAttribute("disabled");
            button.style.cursor = "pointer";
        } else {
            button.setAttribute("disabled", "disabled");
            button.style.cursor = "not-allowed";
        }
    }
    
    // Attach input event listeners to trigger the function when input values change

    
    usernameInput.addEventListener("input", updateButtonState);
    emailInput.addEventListener("input", updateButtonState);
    passwordInput.addEventListener("input", updateButtonState);
    
orchid portal
stuck gust
#

only first one is for this function

#

dont mind bottom two errors, everything works fine 👍

orchid portal
# stuck gust

Is the script tag in the head without a defer attribute?

stuck gust
#

<script src="script.js" defer></script>

#

btw i have html in js

#

i mean

orchid portal
# stuck gust

Line 323 is this?

 usernameInput.addEventListener("input", updateButtonState);
stuck gust
#
 button.addEventListener('click', function() {
        modal.style.display = 'block';
        
        // Customize the modal content here
    
        
        modalContent.innerHTML = `
        <form method="post" action="connect.php" novalidate id="registrationForm">

    
        <p style="text-align: center;"><b>Rejestracja</b></p><br>

        <p style="text-align: left;"><strong>Nazwa Użytkownika</strong></p>

        <input id="username" style="width: 80%; height: 8%; margin-right: 230px;" autocomplete="off" name="name"><br><br>

        <p style="text-align: left;"><strong>Email</strong></p>

        <input id="email" type="email" style="width: 80%; height: 8%; margin-right: 230px;" autocomplete="off" name="email"><br><br>

        <p style="text-align: left;"><strong>Hasło</strong></p>

        <input id="password" type="password" style="width: 80%; height: 8%; margin-right: 230px;" autocomplete="off" name="password">

        <p style="text-align: left; font-size: 14px" id="password-validation">Hasło musi zawierać minimum 6 znaków.</p>

        <button class="transbutton" id="registerconfirm" disabled style="cursor: not-allowed;"><b>Zarejestruj się</b></button>


        

        <p style="position: absolute; top: 94%; left: 10%">Posiadasz już konto?<a href=""> <b style="font-size:18px; color:#b3b3b3  ">Zaloguj się</b></a></p>
  </form>
        
        `;    
    });
orchid portal
stuck gust
#

how can i fix that

#

i must put it into html?

orchid portal
stuck gust
#

lmao

#

it works now 😭

#

tysm chooking love u