Skip to main content
Participant
October 24, 2024
Answered

Custom Number Format includes Minus Sign

  • October 24, 2024
  • 9 replies
  • 1204 views

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.

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer try67

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;
	}
}

9 replies

Participant
November 2, 2024

Can you please eloborate it? I don't understand the logic

try67
Community Expert
Community Expert
November 2, 2024

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.

try67
Community Expert
Community Expert
October 24, 2024

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.

Participant
October 24, 2024

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? 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 24, 2024

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;
	}
}