Hi!
What I am trying to do is Enable a submit button only when there is more than 4 words are inputted in the password text box. I have chosen " onChange" and works 99% well…the only problem is the button enable only AFTER I clicked somewhere else. Can I do I real time checking?
This is my code (work but not real time):
$w.onReady( function () {
$w(“#password”).onChange(ButtonEnableDisable);
});
function ButtonEnableDisable(){
if ($w(‘#password’).value.length >= 4) {
$w(‘#regenter’).enable()
} else {
$w(‘#regenter’).disable();
}
}
I have also tried “onKeyPress()” but not working:
$w(“#password”).onKeyPress((event) => { let pwval = event.target.value;
It turns out that the KeyPress event object has an intuitively unexpected behavior, but there is a way to deal with it. Have a look at this annotated screen shot:
Understanding this, here is some code to make your form work:
export function password_keyPress(event) {
console.log(event);
DisableEnable(event);
}
function DisableEnable(event){
let realValue = "";
switch (event.key) {
case "Shift":
case "Control":
case "Alt":
case "Meta":
// these keys were pressed, so don't add event.key to the realValue variable.
realValue = event.target.value;
break;
case "Backspace":
let PrevValue = event.target.value;
let NewLength = PrevValue.length - 1;
realValue = PrevValue.slice(0,NewLength);
break
default:
realValue = event.target.value + event.key;
break;
}
if (realValue.length >= 4) {
$w('#regenter').enable()
} else {
$w('#regenter').disable();
}
}
Once you have the correct value, you can then parse the string into an array of words. The size of the array is the number of words in the input string.
I know Kana used the term “words”, but how often are multiple words expected in a password input box? I’ve never seen it. The code also suggested that the intended check was for four characters entered. Kana?
Guys thank you very much for the info.! and yes I meant “characters”, sorry about that!
I have tried both suggestions but still I need to CLICK somewhere else to make the button enable…The following is my code using @tony-brunsman 's suggestion, in case I’ve put some codes in a wrong place.
import wixUsers from ‘wix-users’; import wixLocation from ‘wix-location’; import wixWindow from ‘wix-window’;
export function password_keyPress(event) {
console.log(event);
DisableEnable(event);
}
function DisableEnable(event) { let realValue = “”; let fadeOptions = {
“duration”: 500,
“delay”: 0
}; switch (event.key) { case “Shift”: case “Control”: case “Alt”: case “Meta”:
// these keys were pressed, so don’t add event.key to the realValue variable.
realValue = event.target.value; break ; case “Backspace”: let PrevValue = event.target.value; let NewLength = PrevValue.length - 1;
realValue = PrevValue.slice(0, NewLength); break default :
realValue = event.target.value + event.key; break ;
}
And for setTimeout, I have tried putting it in different place but I have no confidence at all. I have also tried to combine in the above code but no luck.