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.
Related
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.
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.
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.
I have a relatively simple set of inputs that need correct validation, and I seem to have a hiccup so far. Upon clicking send, only the empty fields should be filled in with the .errorVal class to give them a red background. So that is the visual side of the validation. Also, I need these fields to have character validation. So, First and Last name cannot contain numbers. Email needs to have an # symbol at some point. Any ideas?
http://jsbin.com/OlUGiXE/1/edit?html,css,js,output
$('.sendSupport').click(function () {
if ($("table.forceCenter > input[type=text]:empty").length == 0 && $("textarea:empty").length == 0) {
//alert('sent')
$('.sentVal').fadeIn();
$('.emptyVal').hide();
// clear values
$("input[type=text], textarea").val('').removeClass('errorVal');
} else {
//alert('empty')
$('.emptyVal, .errorStar').fadeIn();
$('.sentVal').hide();
$("input[type=text]:empty, textarea:empty").addClass('errorVal');
}
});
To check if every input is not empty you can do something like this:
jQuery("input[type=text]").each(function()
{
if($(this).val()=="")
{
$(this).addClass("errorVal");
}
});
To check if an input have a number take a look at the link below:
Check whether an input string contains number
And if an input contains "#" read this Jquery check if input .val() contains certain characters
I have a checkbox which updates totals on a page based on whether it's checked or unchecked. On one part of my form, I would like to zero out a Telerik Numeric TextBox (Text Input field) if the user decides they do not require that category.
When the above checkbox is unchecked, the value in the textbox on the right should change back to zero. To accomplish this, I've written the jQuery code below (note, this function is called whenever something changes on the form):
var zeroValue = getNum(0.00);
//Scaffolding
if ($("#ScaffoldingRequired").attr('checked')) {
scaffoldingCost = getNum($("#ScaffoldingCost").val());
totalCost += scaffoldingCost;
} else {
$("#ScaffoldingCost").val(zeroValue);
$("#ScaffoldingCost").text(zeroValue);
}
//Round&Convert to 'decimal'
function getNum(num) {
var number = parseFloat(num) || 0.00;
var rNumber = Math.round(number * 100) / 100;
return rNumber.toFixed(2) * 1;
}
NOTE: To make sure everything rounds properly and doesn't give me NaN's, I've used my getNum function ( I understand it's a bit hack ).
This code works, and all my totals calculate properly but the textbox does not display a zero value until the user actually clicks on the input box. I've displayed this below:
Is there something I can do to recycle my input box so it displays the zero value immediately? Or am I setting the value/text completely wrong?
You say you're using Telerik, have you considered using their client API for changing the value of the textbox?
var textBox = $('#ScaffoldingCost').data('tTextBox');
textBox.value(0);