javascript for adobe acrobat form - javascript

Trying to write script for adobe acrobat xi form
When the user selects checkbox7 the field should display a value of 239 and if it is unchecked it shows a value of 0
Also I have it set to mouse up (is that correct?)
Below is my code:
var Checkbox7Value = this.getField("Checkbox7");
if (Checkbox7Value.isBoxChecked(239))
var Checkbox7Value = 239
else
var Checkbox7Value = 0
Thanks in advance for your help!

What is the return value of the checkbox? Is it the default "Yes", or anything else?
However, the code is pretty much messed up, I must say…
In the first line, you define a Field Object, linked to the field Checkbox7
In the second line you test whether the 239th widget/occurrence of that field is checked.
Depending on the result, you redefine the Field Object as a number.
Anyways, assuming that you don't have that many calculations, and dependencies, and that the return value of the checkbox field is indeed "Yes", you would add the following to the Calculate event of the field where the result shall appear:
if (this.getField("Checkbox7").value == "Yes") {
event.value = 239 ;
} else {
event.value = 0 ;
}
Now if you know that checking the Checkbox7 field will always mean "239", you can set its return value to 239, and then the code becomes even simpler:
event.value = this.getField("Checkbox7").valueAsString.replace(/Off/gim, "0") ;
We have to do the replace() because the value for "unchecked" is always "Off" in a checkbox/radiobutton.

Related

JavaScript Acrobat Forms If field A has a number entered, the number in field B must be greater

I am trying to create my first ever PDF Form...
I think I have formatted the fields correctly with text, dates, numbers etc.
Now I need to do my first ever java script (I think)
I have a field where a number could be entered (if applicable)
IF it is - I do need a number to be entered in field B which needs to be GREATER than field A.
Can anyone please assist & bear in mind this is my first ever :-)
I know I will also need the same thing re dates - i.e. date must be after, but am hoping that it will be similar
Thanks in Advance
numA.addEventListener('input', function(){
aValue = this.value;
})
numB.addEventListener('input', function(){
bValue = this.value;
validNumB();
})
function validNumB() {
return bValue > aValue;
}
Add the following code to a custom validation script in Field B. You may need to adjust the name of "field A" to match your actual field name. JavaScript is case sensitive so be aware of that when you are editing. You can customize the alert message to suit your needs as well.
if (this.getField("field A").value != "" && event.value <= this.getField("field A").value) {
app.alert("This field must be greater than field A.")
event.rc = false;
}
In case you are curious... The event.value is the value that the user entered into the field, it only gets committed if the event.rc (result code)is true. When event.rc is set to false by the script, the field value reverts to it's previous value.

How To Check For Empty Fields In HTML Form With JavaScript

I'm checking a website registration form with JavaScript code and onchange listeners.
Empty fields/spaces need to be checked for first before checking for illegal characters, too long strings, etc.
I've read this.
But for a null string,
if (field.value ==="")
alert("Empty field!");
this will not generate the desired alert.
People at the end of the above thread suggested that recent browser versions might not accept such a statement.
So, how do I sort out empty/blank/ignored fields ?
EDIT 1
I've already tried
if (!field.value)
but it only provides an alert if the user has already typed some characters in the field and immediately deleted them before entering a blank field. It will not provide an alert just by clicking the mouse on it and then tabbing on to the next field. It looks like I may need to assign a null value to these form fields at the outset . . I am using implicit adding of the changeEvent listener, i.e. on seeing a value explicitly assigned to the onchange attribute of an element, it is activated without any addEventListener(..) statement.
Also,
if (field.value.length == 0)
does not seem to produce any alert.
EDIT 2
Sorted, I think.
I was using the JavaScript null field check as part of a field-by-field validation check on a web form.
I was using onchange as the event handler. This was wrong. What was needed here was onblur since in the case of a completely null field (i.e. a field on which nothing had been entered before tabbing away from it), no change has been effected -- and therefore no onchange event occurs that would trigger a JavaScript alert.
Thanks for your efforts.
I was stuck on this one across a couple of weeks and only sorted it with the help of some experimental programming by a more experienced guy at work here.
In this script you can see an alert of your variable value ( a console.log would be lees noisy :)
The use of === is for type check but in your example does not make sense as you are using an empty string
<script>
var field= {};
checkEquality(field);
field.value = "";
checkEquality(field);
function checkEquality(object){
alert(object.value);
if (object.value === "")
{
alert("===");
}
if(object.value == ""){
alert("==");
}
}
You can use bellow code for check all four(4) condition for validation like not null, not blank, not undefined and not zero only use this code (!(!(variable))) in javascript and jquery.
function myFunction() {
var data; //The Values can be like as null, blank, undefined, zero you can test
if(!(!(data)))
{
alert("data "+data);
}
else
{
alert("data is "+data);
}
}

Acrobat PDF calculation with checkbox

I've created a PDF form to help create an estimate for plumbing work. There are various lines that the contractor fills in for quanity, cost of the item and then automatically calculates the price for that line; this works fine.
But now I want to add a checkbox to the line that the customer would check if they actually want that work to be done. If they check the box then the price would appear in the final field, otherwise it would display 0.
My fields are:
QtyRow2 ItemCostRow2 CheckboxRow2 PriceRow2
I've tried this Javascript code in the Calculation tab for the PriceRow2 field, but it displays "0" in the price field whether the checkbox is checked or not.
var oFldQty = this.getField("QtyRow2");
var oFldItem = this.getField("ItemCostRow2");
if (this.getField("CheckboxRow2").isBoxChecked(0)) {
nSubTotal = oFldQty.value * oFldItem.value;
} else {
nSubTotal = 0;
}
event.value = nSubTotal;
How should I modify this to get it to work?
If this is the whole code in the calculation, it would be way safer to define nSubTotal; otherwise, it gets defined as a global variable, and can behave strangely.
Also, whenever this calculation runs, and the test results to false, nSubTotal is set to 0. That means, you have to define nSubTotal, and then add to it while you work through the form.
If you want to simply have a result in the field, there is no need to do the detour via a variable; you can set event.value in the true and the false path.
For a single checkbox, it is IMHO easier to use its value (or its "unchecked" value for portability of the code reasons). This leads to the following code snippet:
if (this.getField("CheckboxRow").value != "Off") {
// box is checked
event.value = oFldQtyty.value * oFldItem.value ;
} else {
// box is unchecked
event.value = 0 ;
}
And that should do it.
However, as you have a table, it is best practice to consolidate all calculations into one single script, which can be attached to a hidden read-only field which is not even involved in the calculation itself. This gives you much better overview and control over the calculation, and prevents calculation order errors.

Javascript validation when user is focused on text box before commit

JavaScript validation rarely comes up in my job and I am a little rusty.
I have a page which confirms overrides for a procedure.There are three elements of relevance here;
A Check Box (cashCheckBox), which the user checks if he/she wishes to provide input cash
the text box where the user places the actual value (cash) assuming they checked the check box
An Execute button.
Once the check box is checked, is the user clicks anywhere else, the text box displays the default value ($0). However, when the user checks the check box and then immediately selects the text box (providing no input) then subsequently clicks execute, the user may begin a process in which cash is unknowingly selected as "$0".
Is there a simple addition i can add to the if condition to accommodate this possibility?
function Validate(){
var f=document.frmGeneral;
if((f.cashCheckBox.checked) && (f.cash.value=="" || snip(f.cash.value,"$")<0))
{
alert("cash must be greater than 0");
f.cash.focus();
return false;
}
return true;
}
Try this:
function Validate(){
var f=document.frmGeneral;
if(f.cashCheckBox.checked && +f.cash.value.replace("$","") >= 0)
{
alert("cash must be greater than 0");
f.cash.focus();
return false;
}
return true;
}
The main change is to: +f.cash.value.replace("$","").
replace("$","") removes all $ characters
The + preceding f.cash.value will force the string to be converted to a number. So "0" becomes 0, or "abc" becomes NaN
With the input cleaned up it then just does a check to make sure the value is greater than zero. If it isn't (i.e. it is 0 or NaN) then it will fail the check.
UPDATE
Turns out 0 should be a valid value. Changed to >= 0.

Generic way to get the value of an input regardless of type

I'm trying to write a generic function I can use with the jquery validation plugin that will make a field required based on the value of another field. Here's what I want to happen:
If Field 1's value is in a specified array of values (currently testing with "No", "n/a", and "0"), or is empty, do nothing. Otherwise, make Field 2 required.
Getting the value of Field 1 is the issue. I had no problem figuring this out with a text-type or <select> input, but I'm trying to get it to work with radios and having difficulty. Here is an excerpt from my code:
var value = $('[name="option"]').val(),
empty = ['no', '', 'n/a', '0'];
// If the input is not "empty", make the additional field required
if ($.inArray(value.toLowerCase(), empty) < 0) { // returns -1 if not found
required = true;
} else {
required = false;
}
This works for everything I need it to except radios, because it seems to read the value of the first radio, regardless of if it was checked or not.
The field that will trigger the "required" will either be one of the various text inputs (text, url, email, etc.), a <select>, or a single choice set of radios. No multiple choice. I'll be passing this function as a parameter to required in my jquery validation config for each field I want it to apply to. The name attribute of the "other" field that gets evaluated will be available to it.
Here's my demo so far, kind of ugly but there are notes: http://jsfiddle.net/uUdX2/3/
I've tried a bunch of different ways using is(':checked') and the :checked selector, but they have all failed. I removed them from the demo because they didn't work.
What do I need to get this working with radios and text-type or select inputs, without knowing which kind of input it will be?
Try this
var value = $('[name="option"]');
var type = value.attr("type");
if(type && type.toLowerCase() == 'radio')
value = value.filter(":checked").val();
else
value = value.val();
Working demo
Something like this:
var value = $('[type="radio"][name="option"]:checked, [type!="radio"][name="option"]', form).val() || '0'
Quite similar to Shankar's but does it all in the selector.
http://jsfiddle.net/infernalbadger/uUdX2/8/
It's not working when nothing is selected. Not sure what you want it to do when that happens?

Categories

Resources