Validity.valid is acting weird

I have this code to validate an email:

export function email_blur(event, $w) {
console.log(“--------------------------”);
console.log(“email : “,$w(”#email”).value)
let email = $w(“#email”).validity;
if(!email.valid ) {
console.log(“–email.valid false”,email.valid);
$w(“#emailError”).show();
$w(“#email”).focus();
}else{
console.log(“–email.valid true”,email.valid);
$w(“#emailError”).hide();
}
}

if i enter an invalid email it show the error, but if i then, enter a correct one valid still ‘false’, if i try again with any change it turns to ‘true’, like if it where delayed, this is the output for ‘false’,‘true’, ‘true’:

email : test
–email.valid false false

email : test@test
–email.valid false false

email : test@test
–email.valid true true

Any idea why it behaves like this? what i’m doing wrong?

Hi Bernabe,

Your code should be correct. This appears to be a problem on our end.

We’ll investigate further and let you know when we have a solution. Sorry for the trouble!

Any fix to this, I seem to be having delayed validity test as well.

I’ve sent a reminder to the developers to look into this issue, and hopefully there’ll be a solution soon. In the meantime, as a workaround you can use the setTimeout() function with a small delay and check the validity inside the callback function, like this:

setTimeout( () => {
    console.log($w("#email").valid);
}, 100);

Hope this helps.