Forums

Javascript don't work on my text field

hilmata 14 Oct, 2019
Hi. I have my text field that I formatted so that we can fill in the dates of birth. I then put javascript codes to avoid the errors of seizures and that the men of more than 40 years are registered. At first it worked well but now, nothing. On the other hand locally it works. Can you help me please?

​[file=12306]Capture1.PNG[/file]






javascript57
jQuery.fn.form.settings.rules.verifnaissance = function(value, param) {

var dateNow = new Date();
var currentYear = dateNow.getFullYear();
strSaisie = value.replace(/-/g,"");
var yearSaisie = Number(strSaisie.substr(4,4));
if(document.getElementById('sex0').checked){
var valeur = document.getElementById('sex0').value;
} else{
if(document.getElementById('sex1').checked){
var valeur = document.getElementById('sex1').value;
}
}
if(valeur != null && yearSaisie ==0) {
return true;
}

if ((currentYear - yearSaisie)<18 || ((valeur == 'M') && (currentYear - yearSaisie) > 40)) {
return false;
}else{
return true;
}

};
javascript59
jQuery.fn.form.settings.rules.verifNaN = function(value, param) {

str = value.replace(/-/g,"");
var year = str.substr(7,1);

if( year == 'y') {

return false;
}else{

return true;
}

};
GreyHead 14 Oct, 2019
Hi hilmata,

Sounds like a JavaScript error from something else on the page. Please post a link to the form so we can take a quick look.

Bob
hilmata 14 Oct, 2019
OK. http://pjfe.dncmp-togo.com/#sp-header-appointment

thanks for the quick reply

This is private content

healyhatman 14 Oct, 2019
Can you not just use a calendar field, set to Date, with the earlier time set to {date:Y-m-d$-40 years} and the latest date set to {date:Y-m-d$-18 years} ? That will only let you select birth days 18-40 years ago.
hilmata 14 Oct, 2019
yes but the problem is that the age limit of 40 is just applied to men
hilmata 15 Oct, 2019
so how am I going to enter the "Earliest date / time" parameter of the calendar field so that it only applies when sex = M ?
healyhatman 15 Oct, 2019
Have two calendars, with different IDs. use #id to show/enable or hide/disable one field and the opposite for the other, so you have one field with the male rules and one without, and they hide/show depending on which sex you choose. That's one option.
hilmata 15 Oct, 2019
Thank you. I will try and make you a return.
hilmata 16 Oct, 2019
1 Likes
Hello!!! I come with good news!! finally, I retouched ma javascript code I it started to work again.

there is the new contents of my javascript57. I used document.querySelector instead of document.getElementById/ByName to get the value of the date. Thank you!!
jQuery.fn.form.settings.rules.verifnaissance = function(value, param) {

var dateNow = new Date();
var currentYear = dateNow.getFullYear();

var dateNow = new Date();
var currentYear = dateNow.getFullYear();
var strSaisie = document.querySelector("#date_naissance").value.replace(/-/g,"");
var yearSaisie = Number(strSaisie.substr(4,4));


valeur = (document.querySelector('input[name="sexe"]:checked').value);

if(valeur != null && yearSaisie ==0) {
return true;
}

if ((currentYear - yearSaisie)<18 || ((valeur == 'M') && (currentYear - yearSaisie) > 40)) {
return false;
}else{
return true;
}

};
This topic is locked and no more replies can be posted.