Copy link to clipboard
Copied
I know there are a bunch of posts on this subject, but what I am looking for is simple. I am not a JavaScript expert by a long shot.
What I am looking for is that when a user enters a text field and types in say 01232021 and leaves that box, it will be formatted at 01/23/2021
So this time, it does show up, I guess I have to add a space, try this:
if (theDate.length == 8 ) {
Copy link to clipboard
Copied
For that to work, you need JavaScript. You first need to make sure that you have a string of the correct length (and potentially only containing digits), then you break it up into the three parts and assemble again with the correct delimiters. When you use the following as a custom validation script, it should do the job:
var theDate = event.value;
if (theDate.length == 8) {
var month = theDate.substring(0,2);
var day = theDate.substring(2,4);
var year = theDate.substring(4);
event.value = month + "/" + day + "/" + year;
}
Copy link to clipboard
Copied
Thank you Karl for the script. When entering it in, I am getting s SyntaxError: missing : after property ID 3: at line 4.
It looks the same as the other lines, not sure what it is asking.
Copy link to clipboard
Copied
Hmmm.. there is something missing, even though I copied and pasted from Acrobat.
The "if" line should be like this:
if (theDate.length == 8) {
For whatever reason, the forum software took the "8)" and converted it to an emoji (which is not being displayed, but when you copy and paste that line back into the forum editor, it shows up).
Copy link to clipboard
Copied
So this time, it does show up, I guess I have to add a space, try this:
if (theDate.length == 8 ) {
Copy link to clipboard
Copied
Perfect, thank you.
Copy link to clipboard
Copied
Hi Karl,
Hope your still checking.
I need to remove the YYYY from the above script. I tried just removing the var year line and everything after the day in the event.value
When I preview the form now the / does not show. I changed the length to 6 but that did not work either.
Copy link to clipboard
Copied
Can you please post the script you are trying to use.
Copy link to clipboard
Copied
Sorry it was the one up earlier in the string:
var theDate = event.value;
if (theDate.length == 8 ) {
var month = theDate.substring(0,2);
var day = theDate.substring(2,4);
var year = theDate.substring(4);
event.value = month + "/" + day + "/" + year;
}
Copy link to clipboard
Copied
I actually meant the one you modified.
Copy link to clipboard
Copied
It is the same script. I undid the changes.
Copy link to clipboard
Copied
This is what I tempted:
var theDate = event.value;
if (theDate.length == 5 ) {
var month = theDate.substring(0,2);
var day = theDate.substring(2,4);
event.value = month + "/" + day;
}
Copy link to clipboard
Copied
Sorry for the delay, but family and job come first 🙂
Try this:
var theDate = event.value;
if (theDate.length == 4) {
var month = theDate.substring(0,2);
var day = theDate.substring(2);
event.value = month + "/" + day;
}
Copy link to clipboard
Copied
Perfect, thank you