Hello I am trying to create a form on my home page where you have to enter your date of birth before you can enter my website. My problem is I have the form working but I want to block out anyone that is under 18 years of age. How do I do that.
Hey Patrina, was really hoping I would be able to “customize” my date of birth and take a few years off of my age.
You can try this to get the user’s age and then enable or disable the form’s submit button:
$w.onReady(function () {
$w("#datePicker1").onChange((event) => {
let newValue = event.target.value; // "new value"
let age = getAge(newValue);
console.log('age: ', age);
if(age >= 18) {
$w('#submit').enable();
}
else {
$w('#submit').disable();
}
});
});
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
Make sure that you turn off the button’s “Enabled by default” setting:
Thank you for you help, question where do I paste this information. Any exact spot.
@patrinamoore The code that I posted would be pasted in the code panel of your home page.
I am not seeing the properties popup to " enable default"
Click Tools across the top and enable the Properties Panel.
In the Editor, click on the Button and you should see the Button’s property panel:
If you don’t see the Property Panel, make sure it’s enabled:
If you still don’t see the Property Panel, you can contact the Wix support team . They’ll be better able to help you.
Ok great I got that part done. However when I preview it is says
public/pages/masterPage.js: Unexpected token (4:45)
2 |
3 | $w.onReady(function () { >
4 | //TODO: write your page related code here… | ^
Hello I still didn’t get it to work
Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor. Please include the name of the page involved.
Page name Consent (it’s a lightboxes page)
Your code indicates the error:
You are using the wrong ID for the button. Change #submit to #button1.
Ok I changed the #submit to #button1 not I am getting this error
public/pages/masterPage.js: Unexpected token (28:1)
26 | }
27 | return age; >
28 | ] | ^
Also my it is not taking into the website. Can you go onto the page and edit the code for me please I really need to get this up and running.
Hello Yisreal are you able to assist?
I looked at the page and there is no code at all.
You have errors in your code:
You have onReady() function inside of an onReady() function which is invalide.
You need to close the getAge() function with a } and not with a ]
Your page code should look like this:
$w.onReady(function () {
$w("#datePicker1").onChange((event) => {
let newValue = event.target.value; // "new value"
let age = getAge(newValue);
console.log('age: ', age);
if (age >= 18) {
$w('#button1').enable();
} else {
$w('#button1').disable();
}
});
});
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
Ok I updated the code and there is still one error.
Loading the code for the site. To debug this code, open masterPage.js in Developer Tools.
TypeError: $w(…).onChange is not a function Site Code Line 3
Loading the code for the Consent popup. To debug this code, open kx9fo.js in Developer Tools.
I believe your problem is that you are using the DatePicker and the Button inside of a Wix Form. You should not be using a Wix Form and instead you should just have a DatePicker and a Button.
As a separate issue, make sure that you initialize the Button to be disabled.
BTW - you will notice that in spite of the error, the DatePicker onChange() function still gets called and returns the correct age.
Ok take a look at it now. I deleted the form and I have them as two separate issues. When I disable the button you can’t click on it though. Also I still have the same error.