• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Custom Number Format includes Minus Sign

New Here ,
Oct 24, 2024 Oct 24, 2024

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.

TOPICS
JavaScript , PDF forms

Views

313

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 24, 2024 Oct 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;
	}
}

Votes

Translate

Translate
Community Expert ,
Oct 24, 2024 Oct 24, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 24, 2024 Oct 24, 2024

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? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 24, 2024 Oct 24, 2024

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 24, 2024 Oct 24, 2024

Copy link to clipboard

Copied

That worked. Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 01, 2024 Nov 01, 2024

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2024 Nov 02, 2024

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2024 Nov 02, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 02, 2024 Nov 02, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2024 Nov 02, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines