In the preview window it runs regardless of whether data validation is successful or not - i.e. has a field been completed. I will read up further to see if there is any way of only running it if all the fields are validated first.
You can check the validity of the inputs when the button is clicked:
export function button1_click(event, $w) {
const firstNameInput = $w("#input1");
if (!firstNameInput.valid) {
console.log("Invalid first name.");
return;
}
const lastNameInput = $w("#input2");
if (!lastNameInput.valid) {
console.log("Invalid last name.");
return;
}
const emailInput = $w("#input14");
if (!emailInput.valid) {
console.log("Invalid email.");
return;
}
// ...
}
Or you can disable the button until the all inputs are valid:
import wixCRM from "wix-crm";
$w.onReady(() => {
const firstNameInput = $w("#input1");
const lastNameInput = $w("#input2");
const emailInput = $w("#input14");
const button = $w("#button1");
updateButtonAvailability();
[firstNameInput, lastNameInput, emailInput].forEach(input => {
input.onChange(updateButtonAvailability);
});
function updateButtonAvailability() {
if (inputsAreValid()) button.enable();
else button.disable();
}
function inputsAreValid() {
if (!firstNameInput.valid) {
console.log("Invalid first name.");
return false;
}
if (!lastNameInput.valid) {
console.log("Invalid last name.");
return false;
}
if (!emailInput.valid) {
console.log("Invalid email.");
return false;
}
console.log("All input is valid.");
return true;
}
});
// ...
In preview mode it stopped with an error when it picked up a duplicate email.
Could you elaborate? I can’t reproduce.