I am filling a form automatically with a bookmarklet, but I fail to
trigger the validation of the fields. The code is
// fill form field
var my_username="user123";
document.getElementsByClassName("form-field")[0].firstChild.value=my_username;
// simulate click on the submit button
document.getElementsByClassName("form-field")[1].firstChild.click();
When I use the bookamarklet "user123" appears on the screen. The
problem is that this does not trigger the automatic validation that
would be triggered by real keypresses. With real keypresesses the
field gets validated, a tick appears in the screen and the submit
button gets enabled.
Any idea on how to trigger the validadtion and enable click on the
submit button?
On Thu, 29 Jul 2021 09:26:10 -0500, Alfred wrote:
I am filling a form automatically with a bookmarklet, but I fail to
trigger the validation of the fields. The code is
// fill form field
var my_username="user123";
document.getElementsByClassName("form-field")[0].firstChild.value=my_username;
// simulate click on the submit button
document.getElementsByClassName("form-field")[1].firstChild.click();
When I use the bookamarklet "user123" appears on the screen. The
problem is that this does not trigger the automatic validation that
would be triggered by real keypresses. With real keypresesses the
field gets validated, a tick appears in the screen and the submit
button gets enabled.
Any idea on how to trigger the validadtion and enable click on the
submit button?
HTML alone can not enable a disabled submit button, or disabled an enabled one. Only JavaScript can do that. Meaning that the validation is done by JavaScript. By an event handler code.
With some luck, bypassing the validation script and calling the form's `submit()` may be sufficient enough if all form field values are already valid. Or, manually enable the submit button, then call its `click()`
method. This will never work if the script does form data processing rather than just form validation.
Since the script is triggered when a key is pressed, the event would be either `input`, `keypress`, or `keydown`. If it's `input`, it can be simulated by dispatching a newly created `input` event on the form field.
Otherwise it'll be difficult or may not be possible at all, since web browsers do not allow keyboard input events to be simulated. What can be
done is to call the event handler directly. Assuming that the handler function is accessible from the global context, and it does not check the `isTrusted` property of the event.
"<input aria-label="Username" autocomplete="off" class="form-control ng-dirty ng-touched ng-valid" maxlength="40" minlength="6" name="username" pattern="^(?=.*[a-zA-Z0-9]{1,})[a-zA-Z0-9_@.-]*$" placeholder="Username" required="" type="text">"document.getElementsByClassName("form-field")[0].innerHTML
"<input aria-label="Password" class="form-control ng-untouched ng-dirty ng-valid" id="password" maxlength="20" minlength="8" name="password" placeholder="Password" required="" type="password">"document.getElementsByClassName("form-field")[1].innerHTML
Any suggestion for validating the second field? should I use another
type of event? Maybe emulating keyboard input?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 250 |
Nodes: | 16 (2 / 14) |
Uptime: | 84:08:53 |
Calls: | 5,510 |
Calls today: | 5 |
Files: | 11,668 |
Messages: | 5,086,187 |