Validating a Decimal Field

Avatar

Avatar

RoC1909

Avatar

RoC1909

RoC1909

26-04-2011

*** THIS IS A REPOST SINCE I POSTED IN THE WRONG FORUM ***

Hello,

I have a quick question and I hope someone can help me.

I  have a form that contains a table with approximately 10 columns and 20   rows. For two of these columns, the rows (AKA fields) within it are   decimal fields w/ 2 leading and 2 trailing digits. I would like these   fields to ONLY allow a user to enter a number from 1 to 24.(or be left  empty so the range might be 0 to 24), and  if a user attempts to place a  number outside of this range, a validation  message pops up stating so.

I am sure it's easy to do, so if someone can point me in the right direction, I would appreciate it.

Thank you.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

pguerett

Avatar

pguerett

pguerett

27-04-2011

Modified your code below ....note the opening { and closing } brackets that are used instead of the the then and endif structures. Also the this reference is lowercase and refers to the object that has initiated the script .....hence you have to use the rawValue property  as well.

Paul

//Check to see if the user entered something

if (this.rawValue != null) {

     //Check the value entered
     if ((this.rawValue > 24) || (this.rawValue < 1)) {
          //Value is outside the range
          app.alert("You must enter a value between 1 and 24!");
          //Clear the field
          this.rawValue = "";
          //Put the cursor back in the field
          xfa.host.setFocus(this);

     }
}

Answers (6)

Answers (6)

Avatar

Avatar

RoC1909

Avatar

RoC1909

RoC1909

27-04-2011

I see. All these "{, } and !" drive me insane .

Either way, this works like a charm.

Thanks again for all of your help, Paul. It's much appreciated!

Avatar

Avatar

RoC1909

Avatar

RoC1909

RoC1909

26-04-2011

Hey Paul,

Based on your previous reply, would integration work like this:

if (This! <> null) {

//Check the value entered
Then ((this.rawValue > 24) || (this.rawValue < 1))
//Value is outside the range
app.alert("You must enter a value between 1 and 24!");
//Clear the field
this.rawValue = "";
//Put the cursor back in the field
xfa.host.setFocus(this);
}

If I add the snippet, the script doesn't validate.

In VBA, I can churn my way through it, but Javascript is insanely foreign.

Avatar

Avatar

pguerett

Avatar

pguerett

pguerett

26-04-2011

If they check in and out without changing anything the value woudl be null. Youcan check if the field is not null and then do

the test for the 1 and 24 .....make sense?

Paul

Avatar

Avatar

RoC1909

Avatar

RoC1909

RoC1909

26-04-2011

Hey Paul,

I hate opening this thread again since you resolved my issue, but I didn't want to create another "new" thread and crowd the forum.

I noticed one thing, if a person were to click in the cell, and then decide against entering anything, as if they chose the wrong cell, the script still fires off (obviously due to having the script as the EXIT event for the field) and I get the "enter a value 1 to 24" message. Is there any way that I can overcome this with a slight modification to this script so that if a person were to click a cell by accident, and then click out of it without entering anything, the message wouldn't pop up?

Thanks again for all of your help.

Avatar

Avatar

RoC1909

Avatar

RoC1909

RoC1909

26-04-2011

Wow! You rock!

This code works perfectly!

Thanks for the prompt response. I really appreciate it.

Avatar

Avatar

pguerett

Avatar

pguerett

pguerett

26-04-2011

I woudl use code like this on the exit event of each of those fields:

//Check the value entered
if ((this.rawValue > 24) || (this.rawValue < 1)){
//Value is outside the range
app.alert("You must enter a value between 1 and 24!");
//Clear the field
this.rawValue = "";
//Put the cursor back in the field
xfa.host.setFocus(this);
}

If you are a programmer you could make this into a function and call the function from each of thse fields instead.