7
submitted 5 months ago* (last edited 4 months ago) by Rick_C137@programming.dev to c/javascript@programming.dev

~~ cross-posted from: https://programming.dev/post/9179830 ~~

Hi,

I'm loading some content with XHR (aka Ajax) the loaded input elements that have a invalid value assigned are not checked trough the validation process.

so the CSS styling with :invalid is for example not working etc..

is there a way to force the validation process on those elements ?

edit: Browser is Firefox

Thanks.

top 9 comments
sorted by: hot top controversial new old
[-] daisyKutter@lemmy.ml 2 points 5 months ago

Do you have code to share? I need to see what you are doing in order to help you

[-] Rick_C137@programming.dev 1 points 5 months ago* (last edited 5 months ago)

It will not be digest to send all what I use for you to see it (it will lead to TL;DR)

but here an elements (loaded with XHR that give the problem

<input id="something" name="something" type="text" placeholder="aPlaceholder"  minlength="3" maxlength="32" value="this value is more than the limiation of 32 char" autofocus required>

So normally this element should be invalid and a CSS selector of :invalid should match. but it's not the case because the browser seem to not run the validation check on loaded elements... !?

if we edit manually the input , for example removing one character then the validation process kick-in and the CSS selector work etc..

[-] coffee_poops@sh.itjust.works 1 points 5 months ago

Usually validation is only triggered on change or blur events. Changing the value programmatically doesn't trigger these events so you need to trigger them yourself.

[-] Rick_C137@programming.dev 1 points 5 months ago

Thank you @coffee_poops@sh.itjust.works that exactly my question.. how can I trigger the validation process again ?

[-] coffee_poops@sh.itjust.works 1 points 5 months ago

Try form.reportValidity() where form is the form element.

[-] daisyKutter@lemmy.ml 1 points 5 months ago
[-] Rick_C137@programming.dev 1 points 5 months ago* (last edited 5 months ago)

Thank @daisyKutter@lemmy.ml

it's look like that that code only verify the form when submitting.. I need to check the validity when the element has been loaded.

btw checkValidity() do not solve this issue as it return True when the length is longer than maxlength ! O_o I guess this is were is lying the "bug"

My two centFor me this is a huge flaw ! I don't understand how we (dev) are we still developing on "standard" that are so f*ck-up..

[-] daisyKutter@lemmy.ml 1 points 5 months ago

There are several proposed workarounds there, like

https://caniuse.com/constraint-validation

[-] Rick_C137@programming.dev 1 points 4 months ago

Thanks all for your input.

My only workaround was to check programmatically all those input and set them with a setCustomValidity()

Too bad there isn't a method to relaunch the validity across all the page :/

Cheers

this post was submitted on 27 Jan 2024
7 points (100.0% liked)

JavaScript

1700 readers
1 users here now

founded 1 year ago
MODERATORS