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

Keystroke validation for a number with optional commas and decimals

Explorer ,
Jul 27, 2023 Jul 27, 2023

I'm trying to find a keystroke validation for numbers where commas and decimals (0.000) may be entered. Any help would be appreciated. 

TOPICS
JavaScript , PDF
1.2K
Translate
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 ,
Jul 27, 2023 Jul 27, 2023

Allow only numbers, commas and dots.
event.rc = /^[0-9,.]*$/.test(event.change);

Translate
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 ,
Jul 28, 2023 Jul 28, 2023

Why not use one of the built-in options under the Format tab?

Translate
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
Explorer ,
Jul 28, 2023 Jul 28, 2023

I would like for the user to be able to enter 100 or 0.100

Translate
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 ,
Jul 28, 2023 Jul 28, 2023

Yes, they can do that with those settings...

Translate
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
Explorer ,
Jul 28, 2023 Jul 28, 2023

If I select number to 3 decimal places it changes 100 to 100.000.  If I select 0 decimal places, it doesn't allow 0.100

Translate
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 ,
Jul 28, 2023 Jul 28, 2023

Hi,
Here is a custom keystroke script that allows your request.

 

if(!event.willCommit) {
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
	var numberRegEx=/^(\d+(.\d{0,3})?)?$/;
	event.rc=numberRegEx.test(testeChaine);
} else {
	var numberRegEx=/^\d+(.\d{1,3})?$/;
	event.rc=event.value=="" || numberRegEx.test(event.value);
}

 

With this script, if you enter a dot you can add up to 3 digits after. You can modify it to allow the number of digits you wish.

@+

Translate
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 ,
Jul 29, 2023 Jul 29, 2023
LATEST

I see what you mean. In that case, no, the built-in Number format setting will not work for you.

You were given some good options below, though.

Translate
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