#How to make that button will be disabled until all inputs are filled?
77 messages · Page 1 of 1 (latest)
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";
}
}
Would you mind sharing the specific issues you are having with this code?
The updateButtonState() is never called and your comment seems to imply that you expect it to operate reactively, which is not going to happen. This function actually never runs at all.
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.
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 -_-
~~I assume all your inputsmight be undefined, but you should have error messages in that case!
However you can try the optional chaining, so it would not throw on load
usernameInput?.value.trim() !== "";~~
This code still has a problem. You run getElementById() too soon. Run it inside the updateButtonState().
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? 😭
Put the getElementById() statements higher. You are using the values before they are defined.
Why did you add code to run this function when the window loads? That is too soon.
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
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.
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
You moved them too high. Keep them inside the function but as the first items.
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 👍
OK. This looks better. Put some console logs to see if it is correctly detecting when the input is valid.
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);
If you have the script tag in the head, use defer.
done
There is another problem. You have a single function checking the validity of all inputs. The problem is that some inputs will have data before others. You must check every input to see if it has a value before you do this other analysis for validity.
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.
you mean this part?
if (isUsernameValid && isEmailValid && isPasswordValid) {
button.removeAttribute("disabled");
button.style.cursor = "pointer";
Your console log comes before that. Move it into that block.
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);
All of your console logs are being done without the validity check. Put them inside the if block. Also, I think you missed my comment where I said your original decision to move the getElementById() code before the function was actually correct.
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
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.
💀
Disregard that last advice. I see that you redefined outside the function. That also works, but is less efficient.
That looks like you have redefinition of constants.
Define them only once but before the function.
if i put then in function it will redifition but only for checking inputs?
I did not understand that question.
It looks like you are defining the same constants before and after the function. Delete the ones after. Lines 320-322.
Before. Just delete the extra that are after. Do not change the ones before.
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);
OK. What happens when you run this?
only first one is for this function
dont mind bottom two errors, everything works fine 👍
Is the script tag in the head without a defer attribute?
Line 323 is this?
usernameInput.addEventListener("input", updateButtonState);
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>
`;
});
ye
That could be an issue. This doesn't get added to the DOM until that button is pressed. The addEventListener() code that is failing has to be put inside of this event handler.
Try moving all the code that we worked on earlier into this event handler.