Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Validating a Decimal Field

Avatar

Level 2

*** 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.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

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

     }
}

View solution in original post

7 Replies

Avatar

Former Community Member

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.

Avatar

Level 2

Wow! You rock!

This code works perfectly!

Thanks for the prompt response. I really appreciate it.

Avatar

Level 2

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

Former Community Member

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

Level 2

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

Correct answer by
Former Community Member

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

     }
}

Avatar

Level 2

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!

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----