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

JavaScript alert and if

New Here ,
Oct 23, 2024 Oct 23, 2024

I have to fields called 'Lmin' and 'Lmax' with values. 

Another Field 'Given L' is to be filled out, but must be equal to min og max - or in between. 

An alert is suppose to pop of if the enteres value does not meet the specification, and the value will be set to 0. 

I have made the validation for 'Given L' in javascript, but has a few things that keeps bugging me: 

- After activating the alert, the cursor is in the next field. I would like to return to the 'Given L'

 

At the end of the document I have a 'claer all fields'-button. 

It works ok, but the min-alert pops up when I activate it - and then clears the fields like it's supposed to.

 

JaveScript in validation of 'Given L':

var min = this.getField("Lmin").value;
var max = this.getField("Lmax").value;
var nValue = Number(this.getField("Given L").value);
if(nValue=0);
else if(event.value<min){
app.alert("WRONG min");
event.value = 0
};
if(event.value>max){
app.alert("WRONG max");
event.value = 0
};
event.value

TOPICS
JavaScript
2.7K
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
1 ACCEPTED SOLUTION
Community Expert ,
Oct 23, 2024 Oct 23, 2024

Change your script to the following:

var min = this.getField("Lmin").value;
var max = this.getField("Lmax").value;
var nValue = Number(event.value);
if(event.value<min){
if(app.alert("WRONG min")==1)
{
event.value = 0;
event.target.setFocus();
}
}
else if(event.value>max)
{
if(app.alert("WRONG max")==1)
{
event.value = 0;
event.target.setFocus();
}
};

View solution in original post

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 ,
Oct 23, 2024 Oct 23, 2024

Change your script to the following:

var min = this.getField("Lmin").value;
var max = this.getField("Lmax").value;
var nValue = Number(event.value);
if(event.value<min){
if(app.alert("WRONG min")==1)
{
event.value = 0;
event.target.setFocus();
}
}
else if(event.value>max)
{
if(app.alert("WRONG max")==1)
{
event.value = 0;
event.target.setFocus();
}
};
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 ,
Oct 23, 2024 Oct 23, 2024

You didn't use 'nValue'.

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
New Here ,
Oct 23, 2024 Oct 23, 2024

You might be right. I'm new to this JavaScript and trying to learn it by self-study, so I'm making some mistakes and useless stuff on the way - this could be one of them... 

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 ,
Oct 24, 2024 Oct 24, 2024

I think @Nesa Nurani was commenting on the script I provided.  I did defined nValue as event.value but then didn't use nValue in the script.  I accidentally left it in after copy and pasting your script then modifying it.

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
New Here ,
Oct 23, 2024 Oct 23, 2024

Great - thanks!

It works as far as the cursor setting. 

But still, when I use the 'clear all fields'-button, I have the min-alert popping up. 

It's no big thing, just a bit annoying 😉 

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 ,
Oct 24, 2024 Oct 24, 2024

Try this script as a Mouse Up action in your reset button field:

this.calculate=false;

this.resetForm();

this.calculate=true;

this.calculateNow();

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
New Here ,
Oct 24, 2024 Oct 24, 2024

Oh, right! The script has to be on the button - good point!
I've tried the script, but it doesn't work as planned. The case is, that even if the calculation is right, the form should be reset - to start all over. (One filled out, printed - now fill out a new one)

I've tried the script with a little modification: 

this.calculate=false;

this.resetForm();

this.calculate=true;

this.resetForm();

- but it didn't work either way. 

Anyway thanks for given me the idear of putting a script on the button!

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 ,
Oct 25, 2024 Oct 25, 2024

You original said you had a clear-all button, so I assumed it was already in a button.  But most people use the built-in reset fields action.  I like to use the script this.resetForm() script instead, which sets all fields to their default values, usually "".  After I provided the script marked correct, I tested a reset button and didn't have any issues.  You said you were still getting a popup.  Since all calculations run whenever any field value changes, I assumed that the order your fields were resetting was causing the script I provided to run in an order that would cause the popup at that point.  So the button script does this:

this.calculate=false; //stops all calculations from running

this.resetForm(); //resets all fields

this.calculate=true; //restores the hold on calculations

this.calculateNow();// runs all calculations (in case there were others that needed to run)

What exactly do you mean by "didn't work"?  Is popup still running when you click this button?  Is the popup the alert in my script marked correct?  Did you remove your reset action from the button before using the mouse up action I provided?

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
New Here ,
Oct 25, 2024 Oct 25, 2024

Thank you for replying again - I feel like quite a noob here. 

I had made a button with the built-in reset function - I just didn't take it in consideration regarding scripts. You helped me to do that. 

 

There are two fields to consider: 

*A drop down - when selected a value here, the Lmin and Lmax are calculated (using if-function)

*GivenL where they write a number (An alert checks if the number is between Lmin and Lmax)

When using the clear-all-button I get the alert (WRONG min) - and afterwards the fields are cleared as wanted. 

I thought it was because the dropdown is cleared first, causing Lmin and Lmax to be 0 - and the the value was to high. But since it's the min-alert that is triggered, it must be the other way around. 

I guess I need to sort out, how to allow tha number to be 0 - not just as a start-value, but also as an input-value. 

 

 

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 ,
Oct 25, 2024 Oct 25, 2024

Try moving the GivenL script to validation instead of calculation.

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
New Here ,
Oct 25, 2024 Oct 25, 2024

It was in validation. 

I tried moving it to calculation just now, which just moves the problem: 

Now the alert comes when selecting a value in the dropdown - but there is no alert when using the clear-all button. 

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 ,
Oct 25, 2024 Oct 25, 2024

Are you able to share your form so I can troubleshoot it?

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
New Here ,
Oct 26, 2024 Oct 26, 2024

The file is attached. 

The dropdown is in 'Vikar som:'

The names of the fields differ from what I've written in my topic: 

Lmin/max = 'Timeløn min/max'

GivenL = 'Aftalt Timeløn'

And the alert has a drifferent text. 

(I changed them in this topic since they all contain one of our danish letters: Ø)

 

So to be clear: 

When a value is chosen in dropdown, a minimum and maximum value is calculated. 

The value in 'Aftalt timeløn' is set to be eguaul to min/max or in between. 

The button called 'Nulstil' is suppose to clear all filds withour a warning. 

The bug is, that the button gives a warning regaring 'Aftalt timeløn' being below minimum. 

 

Thank you for looking into it! 

I've been reading a bit on your homepage and can see I have a long way to go, learning all this code in adobe. 

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
New Here ,
Oct 26, 2024 Oct 26, 2024
LATEST

BTW: It's no rush. 
It's a form from my dayjob, and I'm not suppose to look into it untill monday, so just take your time. 

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