So, moving on from my last post I was able to create a custom multi step form by adding java to a Wix slider, input elements and buttons. The form has functioning next and back buttons and works well. However, I want to disable the next button until the input fields have been filled in so that there are no errors when it comes to submission at the end. Any ideas?
something like thisβ¦
if ((($w(β#input1β).value) === null ) ||
(($w(β#input2β).value) === null ) ||
(($w(β#input3β).value) === null ))
{
$w('#nextButton').disable();
} **else** {
$w('#nextButton').enable();
}
Hi!
You can always set certain fields as βrequiredβ and so it will force the user to complete/fill those fields before the form is finally submitted.
You can turn a field to a βrequiredβ on by entering the component settings and check the relevant checkbox.
Doron.
[@dylanmoss1995] Mikeβs suggestion is closer to what you are looking for. Doronβs solution works as a way to ensure that all fields are complete and, with validation, contain the expected data formats.
What you need to do on top of this is catch an event that allows you to test to see if you can execute the code Mike is proposing. To do that you should add an event handler to each required field that responds to onChange. You can use the same handler for each input element you need to check since it will check all element values.
Steve
Hi Don,
The fields are are set to required but that doesnβt stop users changing to the next slide and then they canβt submit at the end. That method also means they have to go back through the form to find out what the errors are.
The method Mike posted is what I was thinking of, Iβll give it a go and let you know if it works Mike. Thank you both for the suggestion.
Thanks Steve. Itβs been a long time since iβve used code, Iβll give it a go and get back to you!
Update
I added IF/ELSE statement to the onclick function of the next button. As shown below
If (($w(β#fnameβ).value) === ββ) {
$w(β#button1β).disable();
} else {
$w(β#button1β).enable();
$w(β#slideshowβ).changslide(1);
}
I did try .value === null but I had no luck. However, with the code shown above I was able to disable the button but it changed slide first and then disabled. So, when I click back to slide 1 the next button was disabled.
if you are want to go that route then code something like thisβ¦
$w.onReady( function () {
$w(β#button1β).disable();
$w(β#button1β).onClick( function () {
$w('#slideshow').changslide(1);
})
})
export function fname_change(event, $w) {
If (($w('#fname').value) !== ββ) {
$w('#button1').enable();
} **else** {
$w(β#button1β).disable();
}
}
I tried the suggestion above which worked but only for the first name field but I cant make it work for the last name field too. Iβve tried multiple ways to write code that says:
If the first name, last name and date of birth are empty - disable next button. If they arenβt empty - enable next button.
So far, just with the first name field this code works. Any ideas on including the other input fields in it?
$w.onReady( function () {
$w(β#button1β).disable();
})
export function fname_change (event) {
if ((($w(β#fnameβ).value)=== null )) {
$w(β#button1β).disable();
} else {
$w(β#button1β).enable();
}
}
Wix is such a pain in the ass when trying to copy code, every time i try to copy your code from above it pastes in an unformatted mess, very fustrating, i wish Wix would hold the formatting when copying code from the forum, anyway you need to add parameters for the last name as in your code you only have parameters for first name, code something like thisβ¦
$w.onReady( function () {
$w(β #button1 ').disable();
})
export function fname_change (event) {
if ((($w(β #fname β).value)=== null ) ||
(($w(β # l name ').value) === null ))
{
$w(' [#button1](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23button1) ').disable();
} else {
$w(β #button1 ').enable();
}
}
export function lname_change (event) {
if ((($w(β #fname β).value)=== null ) ||
(($w(β # l name ').value) === null ))
{
$w(' [#button1](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23button1) ').disable();
} else {
$w(β #button1 ').enable();
}
}
Thatβs what I have done here - the issue is when I type something into the first name field, the next button is enabled, before I even type anything into the last name field.
Try change null to ββ
@mikemoynihan99 that has stopped the next button enabling after only filling in the first name, but itβs not enabling the button after filling in the last name either now
replace the onChange events with keyPress events:
example:
export function lname_keyPress(event) {
if ((($w(β #fname β).value)=== ββ ) ||
(($w(β # l name β).value) === ββ ))
{
$w(β #button1 β).disable();
} else {
$w(β #button1 ').enable();
}
}
@mikemoynihan99 I wish I could say it worked but itβs doing the same thing. Iβm running out of ideas, maybe theres an issue with six supporting some code? Should I add you as a controbutor so you have access to try figuring it out?
Here is the full form code. The form functions really well apart from this issue with the next button. Only the first four sections of the code are relative to the first page that weβve been working on.
$w.onReady( function () {
$w(β#nextbtnβ).disable();
})
export function fname_keyPress(event) {
if ((($w(β#fnameβ).value) === ββ) ||
(($w(β#lnameβ).value) === ββ)) {
$w(β#nextbtnβ).disable();
} else {
$w(β#nextbtnβ).enable();
}
}
export function flname_keyPress(event) {
if ((($w(β#fnameβ).value) === ββ) ||
(($w(β#lnameβ).value) === ββ)) {
$w(β#nextbtnβ).disable();
} else {
$w(β#nextbtnβ).enable();
}
}
export function nextbtn_click(event) {
$w('#slideshow').changeSlide(1);
}
export function backbtn1_click(event) {
$w(β#slideshowβ).changeSlide(0);
}
export function nextbtn2_click(event) {
$w(β#slideshowβ).changeSlide(2);
}
export function backbtn2_click(event) {
$w(β#slideshowβ).changeSlide(1);
}
export function nextbtn3_click(event) {
$w(β#slideshowβ).changeSlide(3);
}
export function addeducation_click(event) {
$w(β#addeducationβ).hide();
$w(β#education2β).show();
}
export function closeeducation2_click(event) {
$w(β#addeducationβ).show();
$w(β#education2β).hide();
}
export function backbtn3_click(event) {
$w(β#slideshowβ).changeSlide(2);
}
export function nextbtn4_click(event) {
$w(β#slideshowβ).changeSlide(4);
}
export function addsubject_click(event) {
$w(β#addsubjectβ).hide();
$w(β#subject2β).show();
}
export function closesubject2_click(event) {
$w(β#addsubjectβ).show();
$w(β#subject2β).hide();
}
export function closesubject3_click(event) {
$w(β#addsubject3β).show();
$w(β#subject3β).hide();
}
export function addsubject3_click(event) {
$w(β#addsubject3β).hide();
$w(β#subject3β).show();
}
export function backbtn4_click(event) {
$w(β#slideshowβ).changeSlide(3);
}
export function nextbtn5_click(event) {
$w(β#slideshowβ).changeSlide(5);
}
export function preferpaypal_click(event) {
$w(β#bankdetailsβ).hide();
$w(β#paypalemailβ).show();
$w(β#preferpaypalβ).hide();
$w(β#preferbankβ).show();
}
export function preferbank_click(event) {
$w(β#paypalemailβ).hide();
$w(β#bankdetailsβ).show();
$w(β#preferpaypalβ).show();
$w(β#preferbankβ).hide();
}
export function backbtn5_click(event) {
$w(β#slideshowβ).changeSlide(4);
}
you have a typo in this part of your code, you have the ID for the last name input referenced as two different IDβs
export function flname_keyPress(event) { if ((($w(β #fname β).value) === ββ) || (($w(β #lname β).value) === ββ)) { $w(β #nextbtn β).disable(); } else { $w(β #nextbtn ').enable(); }
Rectified, no difference though
I know, i had to modify the code a bit. Itβs working now like thisβ¦
$w.onReady( function () {
$w(β#nextbtnβ).disable();
$w('#fname').onKeyPress( **function** () {
if ((($w(β#fnameβ).value) === ββ) ||
(($w(β#lnameβ).value) === ββ)) {
$w(β#nextbtnβ).disable();
} else {
$w(β#nextbtnβ).enable();
}
})
$w('#lname').onKeyPress( **function** () {
if ((($w(β#fnameβ).value) === ββ) ||
(($w(β#lnameβ).value) === ββ)) {
$w(β#nextbtnβ).disable();
} else {
$w(β#nextbtnβ).enable();
}
})
$w('#nextbtn').onClick( **function** () {
if ((($w(β#fnameβ).value) !== ββ) &&
(($w(β#lnameβ).value) !== ββ)) {
$w('#slideshow').changeSlide(1);
}
})
})