Copy link to clipboard
Copied
I am creating a fillable form which includes a Course Credits field that can be inputed as a fixed number or a range of numbers (example: 3, 6, 1-3, 3-6, 1-18, 12-18, etc.). I haven't been able to a find anything that would help as the number is not a specific format but varies. I need a custom number format that can include the minus sign. I am assuming a custom script but can't figure out the script to use.
Copy link to clipboard
Copied
I understand now. Try this code as the field's custom Validation script (set the Format to None):
if (event.value) {
if (/^\d+(-\d+)?$/.test(event.value)==false) {
app.alert("Invalid value. Enter a number or range.");
event.rc = false;
}
}
Copy link to clipboard
Copied
Why do you need a Format script for? Do you want to change the value the user enters? Or do you mean a Validation script, to reject invalid values? If the latter, you will need to define exactly which values should be allowed, and which shouldn't be.
For example, is this allowed? -3--4 ? Or -3-4 ? Or 3--3 ? etc.
Copy link to clipboard
Copied
I only want a number entered into the field as it the number of a credits for a course. The majority of courses are a fixed number such as 3, 6, or 12, credits. We have some courses that vary in the number of credits. A course could be 1-18 credits.
If I select the format category as a Number, then I can't enter a number range as - is not allowed. I'm unable to enter the number 1-18.
I only need numbers and the minus sign. Is there a way to format the field so others can enter 3, 4, 5, 6, 9, 12, 1-18, 3-6, 12-18, or any variation?
Copy link to clipboard
Copied
I understand now. Try this code as the field's custom Validation script (set the Format to None):
if (event.value) {
if (/^\d+(-\d+)?$/.test(event.value)==false) {
app.alert("Invalid value. Enter a number or range.");
event.rc = false;
}
}
Copy link to clipboard
Copied
That worked. Thank you!
Copy link to clipboard
Copied
How would I adjust the code if I also need a . in the number. Example: 25.50, 50.00-125.50, etc. Thanks!
Copy link to clipboard
Copied
Try this:
if (event.value) {
if (/^\d+\.\d+(-\d+\.\d+)?$/.test(event.value)==false) {
app.alert("Invalid value. Enter a number or range.");
event.rc = false;
}
}
Copy link to clipboard
Copied
The logic is the same. The only thing that changed is the RexExp, to accommodate for a period followed by more digits, as you asked for.
Copy link to clipboard
Copied
Can you please eloborate it? I don't understand the logic
Copy link to clipboard
Copied
Which part don't you understand? The entered value is tested against a Regular Expression to make sure it has the correct format. If not, an error message is shown and the value is rejected.