0

I have this javascript that works well in my PDF form to set a field as required if another field contains data. However, I want to have it ignore a value of "0.00" for the test. But I do not know what /^\s*$/ means let alone how to alter the script for my condition.

var rgEmptyTest = /^\s*$/;
// t is the value to be tested and f is the field to set accordingly
function testMyField (t, f) {
if (rgEmptyTest.test(t)) {
this.getField(f).required = false;         
} else {
this.getField(f).required = true;         
}
}

Thank you!

3 Answers3

1

In your piece of code there is a function that uses regex

A javaScript regExp reference for you.

Thanks @j08691 for the link that explains and let you test the regex used (regexr.com/3rf9u).

You can change your code like this to make a logical exception

var rgEmptyTest = /^\s*$/;
var rgTest = /0.00/;
// t is the value to be tested and f is the field to set accordingly
function testMyField (t, f) {
  if (rgEmptyTest.test(t) || rgTest.test(t)) {
    this.getField(f).required = false;         
  } else {
    this.getField(f).required = true;         
  }
}

I guess it should work

0

\s means space * Means any number " " This is empty " " This too

0

I think I got this to work:

var rgEmptyTest = /^\s*$/;
var rgTest = /^[0\.00]$/;
// t is the value to be tested and f is the field to set accordingly
function testMyField (t, f) {
if (rgEmptyTest.test(t) || rgTest.test(t)) {
this.getField(f).required = false;         
} else {
this.getField(f).required = true;         
}
}

Thank you @Higor Lorenzon and @j08691!

  • `^[0\.00]$` does not work the way you think. It tests for a single character `0` *or* `.` (which is unnecessarily escaped as it is inside a character class). Even if you explicitly test for `^0.00$` it will *only* match that exact string, but not `0.0` or, indeed, plain `0`. Parse the string to a number and test if it's zero, done. – Jongware Jun 22 '18 at 22:02
  • @usr2564301 The field's default value is '0.00' which is why I need to exclude it explicitly, the field value can never be '0' as it is a calculated field with answers to two decimal places. But I thought I had tried '^0.00$' but I will try it again. Thank you for the feedback, I am very grateful. – Bobby Jones Jun 23 '18 at 23:04
  • Apologies – for a real *exact* test you need `^0\.00$`, because the `.` character is special when not escaped. – Jongware Jun 24 '18 at 00:12