I have a "numeric field" which will contain a 10 digit number; the fist 4 digits being the date of birth (DDMMYY).
I would really like to have another field where the DOB is automatically calculated from this. I'd appreciated any help - I have pretty much zero knowledge of scripting.
Solved! Go to Solution.
Views
Replies
Total Likes
ok, I think I can see the problem. CHI is a numeric field and not a text field. Therefore the CHId is not a string but a digital number - that means that "0101191234" is actually a numeric value of 101191234. That cannot be dismantled with substring.
Change the CHI field to textfield and then try again.I tested your fields with the correct type and then it works:
To force format and length of CHI limit max characters
Display and validation patterns text{9999999999} and error on validation
In the calculate event of the second text field I have the following javascript code:
var id = TextField1.rawValue; //10 digit id from 1st field
id.substring(0,2)+"-"+id.substring(2,4)+"-"+id.substring(4,6)
The substring extract the day, month and year - how you combine them in the date string is up to you.
Views
Replies
Total Likes
Views
Replies
Total Likes
ok, age calculation
TextField 2 is set in calculate event like that
var id = TextField1.rawValue;
"19" + id.substring(4,6)+"-"+id.substring(2,4)+"-"+id.substring(0,2)+"T00:00:00"
BUT your approach has a weakness - 2 digit years cannot be interpreted correctly 20 can be 1920 and 2020 - you may need either 4 digits or come up with some rule how to detect the century. Javascript uses 120 for 2020 and 20 for 1920.
Calculate in Age Field:
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());
var birthDateString = TextField2.rawValue;
var birthDate = new Date(birthDateString);
// calculate years
var years = (today.getFullYear() - birthDate.getYear());
// adjust years depending on months
if (today.getMonth() < birthDate.getMonth() ||
today.getMonth() == birthDate.getMonth() && today.getDate() < birthDate.getDate()) {
years--;
}
years;
Views
Replies
Total Likes
Here's what I would do.
Put it to the exit event of your numeric field.
function calculateAge(aDate) {
var oDateOfBirth = new Date(aDate[0], aDate[1] - 1, aDate[2]),
iDiff, oAge,
oDate = new Date(); // get current date
oDate.setHours(0, 0, 0); // reset hours, minutes and seconds
oDate.setMilliseconds(0); // reset milliseconds
iDiff = parseInt(oDate.valueOf(), 10) - oDateOfBirth.getTime() - 1,
dAge = new Date(iDiff);
return Math.abs(dAge.getUTCFullYear() - 1970);
}
var cValue = this.rawValue,
aDate = [cValue.substring(4,6), cValue.substring(2,4), cValue.substring(0,2)],
nAge = calculateAge(aDate);
Textfield1.rawValue = nAge; // show age in Textfield1
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Thanks radzmar. I replaced "Textfield1" with "form1.Page1subform.PatientSubform.AgeCalculated" in your script, but no age is outputted. Just to confirm, the first field is a numeric field, and the output field is a text field with value type "user entered - optional"?
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
You're right radzmar, they're both in the same subtree. I think I just found the problem - there was a validation pattern set to the CHI field - when I removed this, it works! But now the user can enter the wrong number of digits. I do need a validation pattern to make sure that the user enters exactly 10 digits.
Views
Replies
Total Likes
Views
Replies
Total Likes
ok, I think I can see the problem. CHI is a numeric field and not a text field. Therefore the CHId is not a string but a digital number - that means that "0101191234" is actually a numeric value of 101191234. That cannot be dismantled with substring.
Change the CHI field to textfield and then try again.I tested your fields with the correct type and then it works:
To force format and length of CHI limit max characters
Display and validation patterns text{9999999999} and error on validation
CHI is actually a text field already! I'm not sure why I said it was a numeric field, sorry! I actually got it working on the advice of radzmar, though I still need to set a validation pattern to make sure that the user enters a 10 digit number.
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies