Javascript || not working - javascript

I'm Working on a banking application in which i have one drop-down box to select Mode of Payment and i have two text fields for Acc No and Bank Name.
I have three inputs for Mode of payment: 1.Bank Transfer,2.Cash and 3.DD/Cheque.Now I would like to display a pop up message If the user selects the Mode of payment as Bank Transfer or DD/Cheque and leaving the two text fields i.e.,Acc No and Bank Name as blank.
MY JavaScript:
if ($F('personnel_bank_detail_mode_of_payment') == 'Bank Transfer' || 'DD/Cheque')
{
if ($F('personnel_bank_detail_account_number') == '')
{
"* Please Enter Bank Account Number\n";
}
if ($F('personnel_bank_detail_bank_id') == '')
{
"* Select Bank Name\n";
}
}
The above code is working if the user selects Bank Transfer but it is not working for DD/Cheque. How Could i make my code to work for both.Any useful suggestions will be appreciated.
Thanks!

You need to repeat what you're comparing.
if ($F('personnel_bank_detail_mode_of_payment') == 'Bank Transfer' || $F('personnel_bank_detail_mode_of_payment') == 'DD/Cheque')
If you're having some trouble with this, you can make this more obvious to yourself in the future by using parentheses around every statement.
if ((something == otherthing) || (something == someotherthing))
For the behaviour you're expecting, both sides of the || need to be something that evaluate to a boolean, which the == operator returns.

The if condition should rather be :
if ($F('personnel_bank_detail_mode_of_payment') == 'Bank Transfer' || $F('personnel_bank_detail_mode_of_payment') == 'DD/Cheque') {
// do your stuff
}
There is definitely something more you need to know about Logical Operators in JavaScript here.

Related

How to make window.prompt fields required in javascript

I am taking input using window.prompt. There are three prompt one after another. I want to make every prompt box required like we do in form fields. Is there any way to achieve that facility in javascript?
I think you can inspire from below code for every prompt:
let input = prompt('Do something?');
if (input === null || input.trim() === "") {
// execute certain action
}

Javascript validation of Adobe form (radio buttons)

I'm having difficulty (I'm new to JavaScript) figuring out a little validation in Adobe LiveCycle forms. I have a first choice (4 option) radio button, 2nd choice (same 4 options) and 3rd choice (same 4 options) where I'd like a validation to make sure the user doesn't enter the same result 3 times.
I thought it would be something like:
event.rc = true;
if ( form1.#subform[0].FirstChoice.rawValue != form1.#subform[0].SecondChoice.rawValue ! && form1.#subform[0].FirstChoice.rawValue != form1.#subform[0].ThirdChoice.rawValue !)
{
app.alert("You need three separate answers, you dimwit!");
event.rc = false;
}
Evidently, I am being a dimwit and going about this all wrong, but I've drawn a blank.
I was thinking also along the lines of:
form1.#subform[0].FirstChoice.rawValue <> form1.#subform[0].SecondChoice.rawValue ! && form1.#subform[0].FirstChoice.rawValue !<> form1.#subform[0].ThirdChoice.rawValue !)
but I don't know where to go with it.
Help (please), thanks.
You are pretty close. Try:
if ((Select1.rawValue != null && Select1.rawValue == Select2.rawValue) || (Select2.rawValue != null && Select2.rawValue == Select3.rawValue) || (Select3.rawValue != null && Select1.rawValue == Select3.rawValue))
{
app.alert("You need three separate answers, you dimwit!");
}
You need to cover the case where the Selections are not yet filled in. Select1, Select2, and Select3 are the RadioButton group.
I would put this as a calculation on a hidden field since you want it to recalculate whenever a change is made to the radio buttons.

How do I write a script for a IF/OR statement for multiple instances?

I'm trying to write a script rule that's like the following:
if (Field(A) == "Yes" || Field(B) == "Yes" || Field(C) == "Yes")
return "TITLE"
else
return ""
I have it functioning if I'm only doing "if A OR B", but does not work if I add another field into the mix.
What I'm trying to accomplish is offering a list of items that a user has to choose "yes" or "no" for. If ANY of the three is a "yes", I need the title to show up. So if NONE of them is a "yes", then the title will not show up.
Any suggestions? I've searched around multiple sites and cannot find anything that works.
That code should work, so if a given field is not working, then the value for that field is probably not what you're expecting it to be. I suggest using console.log(myFirstVar, mySecondVar, myThirdVar) immediately before the if statement to see what your values are before they hit the if.
Also, it's good practice to use === instead of ==. They usually resolve in the same way but == will return a match in many cases that === does not. Specifically == treats all truthy and falsey values as equal, such that if you're doing an if (myVar == false) check, you'll get true if myVar is 0, false, undefined, etc.
Your code just work fine you can Play with the following Fiddle to see that what are you doing is right.
Using span for displaying the change on test.
HTML :
<span id="title"></span>
JS :
var fieldA = 'Yes';
var fieldB = 'No';
var fieldC = 'No';
if (fieldA == "Yes" || fieldB == "Yes" || fieldC == "Yes")
document.getElementById('title').innerHTML ="TITLE";
else
document.getElementById('title').innerHTML = "";

charCodeAt() working with some validations, but not some other validations

I could have sworn I validated everything properly until my teacher decided to look for every single possible combination of characters and somehow doc me for 10 points. So I went in my code and try to fix the validation but, even though this is just like the example in his lecture outline, it decides to let a1 unwantingly pass the form. I double checked the ascii char code chart using this link
and the 1 still keeps getting through. this is my code section:
else if(f1.state.value.length != 2 ||
!( (f1.state.value.charCodeAt(0)>=65 && f1.state.value.charCodeAt(0)<=90) ||
(f1.state.value.charCodeAt(1)>=97 && f1.state.value.charCodeAt(1)<=122) ))
{
alert('Please enter a state in abreviated form');
f1.state.focus();
return false;
}
why does the 1 keep getting through?
edit: also, i notice 1a works (or doesnt work), but not a1..
interesting, i took the ! out and put it around each individual parenthesese to ! and it worked.. so it was returning true if one or the other was true... then !ing it.
so it should have been :
else if(f1.state.value.length != 2 ||
( !(f1.state.value.charCodeAt(0)>=65 && f1.state.value.charCodeAt(0)<=90) ||
!(f1.state.value.charCodeAt(1)>=97 && f1.state.value.charCodeAt(1)<=122) ))
{
alert('Please enter a state in abreviated form');
f1.state.focus();
return false;
}

Javascript only checks one field

Ok so I've been stumped on this one for days and its frustrating me. (Will frustrate me even more if it's something simple I'm overlooking).
I have a form generated in PHP which I want to verify that certain pieces are filled out. I do this via a JavaScript check when the user clicks the submit button.The JavaScript code is below:
<script language="JavaScript">
function checkFields()
{
if (document.getElementById('ldescription').value == '' || document.getElementById('uname').value == ''
|| document.getElementById('sdescription').value == '' || document.getElementById('email').value == ''
|| document.getElementById('platf').value == "Select Group" || document.getElementByID('cate').value == "Select Category" )
{
alert("Please fill out all of the starred (*) items" );
return false;
}
}
</script>
For some reason though this only checks the ldescription field. If that field has text but all the others are empty it carries on like everything was filled out. Also if I change the order of my checks and ldescription is anywhere but the first check, it will do no check whatsoever even when all the fields are empty.
Any ideas?
EDIT:
Got it fixed. Along with the suggestion I marked as correct the document.getElementById('item').value command worked with only textarea boxes but not regular text input boxes. By changing the command to document.MyForm.myTextName.value everything fell into place.
Couple of problems i noticed with your sample code.
The last getElementById call has improper casing. The final d is capitalized and it shouldn't be
Comparing the value to a string literal should be done by === not ==.
JSLint complains there are line break issues in your if statement by having the line begin with || instead of having the previous line end with ||.
The first and third items are most likely the ones causing your problem.
Inside your if condition, when you are breaking a line, make sure that the last token in the line is the OR operator ||.
Javascript does semicolon insertion, so it may be that semicolons are being inserted (automatically, invisibly, by the interpreter) in a bad place.
Try the below code
<script language="JavaScript">
function checkFields()
{
if (document.getElementById('ldescription').value === '' ||
document.getElementById('uname').value === '' ||
document.getElementById('sdescription').value === '' ||
document.getElementById('email').value === '' ||
document.getElementById('platf').value === "Select Group" ||
document.getElementById('cate').value === "Select Category")
{
alert("Please fill out all of the starred (*) items" );
return false;
}
}
</script>
Please use Javascript && operator which returns true if both the elements are true. || operator evaluates to true in case atleast one of the element is true which is what is happening in your case. You can take a look at Javascript boolean Logic

Categories

Resources