if condition on radio button check/uncheck - javascript

I am very new to javascript and i am trying to insert multiple conditions in an if statement.
below my js.
if (($("#chkIs_3").is(":checked")) && ($(document.getElementById("two").checked == false) && $(document.getElementById("four").checked == false)))
{
alert("Please check one vehicle type.");
}
If and only if the id's(two, four) are unchecked the alert has to be shown, but for me even if one of the radio buttons is checked the alert is shown.
Please help me in rectifying the probelem.
id = chkIs_3 is a checkbox
id = (two & four) are radio buttons.

You are mixing native Javascript with jQuery, for a start. You're also doing
$(document.getElementById("two").checked == false)
...which is just weird and not valid. The dollar sign $ is just a normal variable name. In this case jQuery is using it as its main function name.
This is what you need instead (in case you're not aware, the exclamation mark ! means "not" and inverts the result of the $("#two").is(":checked")):
if ($("#chkIs_3").is(":checked") && (!$("#two").is(":checked") && !$("#four").is(":checked")))
{
alert("Please check one vehicle type.");
}

Related

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.

Issues with Alert Boxes javascript

I have two functions i want on my on change event of a combo box, as below:
onchange="border_shaped();borderchk();"
I have two functions that check for a value and display a message box alert if matched.
The code works fine, apart from, whichever function is second in the onchange event, the alert box dissapears after being displayed.
So as above, the borderchk(); function displays the alert message box for all of half a second then the form reloads. See below functions:
function borderchk()
{
var bordercolour = document.designer.border_id.value;
if (bordercolour == 145)
{
alert("Border Colour 145");
}
else if (bordercolour == 10100)
{
alert("Bordercolour 10100");
}
}
function border_shaped()
{
var sizerear1 = document.designer.size_back.value;
var bordercolour1 = document.designer.border_id.value;
if (sizerear1 == 10049 & bordercolour1 == 144)
{
alert("Border shaped");
function borderchk();
}
}
Ok, not sure why your form reloads without seeing the form HTML. I"m guessing it may be submitting but not sure.
Aside from that you have 3 errors in your javascript you posted:
This line if (sizerear1 == 10049 & bordercolour1 == 144)
should be changed to this if (sizerear1 == 10049 && bordercolour1 == 144)
This line function borderchk();
should be changed to this borderchk();
This is not really an error, but I would put all the numbers in double quotes since you are comparing them to STRINGS not NUMBERS. The values you get from form elements are STRINGS not NUMBERS, although == still seems to be working it might be safer to compare STRINGS to STRINGS

Jquery conditional && not showing expected result

I'm trying to validate a complicated form. In this example I'm checking all radio buttons have a value
if ($('input[name=brand]:checked').val()!="" && $('input[name=section]:checked').val()!="" ) {
alert("both selected all is well");
$("a.gobutfton").addClass("ok");
} else{
alert("They are still not all selected");
}
even if just one is clicked I get the "both are selected" alert (there for testing)
given that I'll have multiple items and form types I can't even get two to behave how I'd imagined they should
http://jsfiddle.net/Lrz8vrdd/1/
One or both are "undefined". Try
if ($('input[name=brand]:checked').val() && $('input[name=section]:checked').val())
That makes sure that neither one is 0, empty string, undefined or null.

Simplify this If statement so it doesn't duplicate code - Javascript

I have an If statement that runs within a 'for' loop to create markers for a Google map. Basically if a tick box is not ticked it runs the code all displays all markers, but if a checkbox is ticked then it checks for a 2nd parameter and only runs the code for the items that match this condition.
The code I have below works fine, but it means I have to use the same code in both parts of the if statement. Apart from building it into a function if there a better way of structuring the If statement to achieve this?
if(!FriendlyChecked){
//Code here
} else if(FriendlyChecked && Friendly == "Yes"){
//Same code here
}
If FriendlyChecked is false, the first condition is satisfied and the code will be executed.
Thus, if the second condition is reached, FriendlyChecked must be true, so you don't need to check it at all, you only need to check that Friendly == "Yes".
if(!FriendlyChecked || Friendly == "Yes"){
// code here
}
if( !FriendlyChecked || (FriendlyChecked && Friendly == "Yes") ) {
// your code
}
!FriendlyChecked || (FriendlyChecked && Friendly == "Yes") will check for either FriendlyChecked is false (not checked)
OR FriendlyChecked is true an value of Friendly is Yes
This will solve your problem
if((!FriendlyChecked) ||((FriendlyChecked)&&(Friendly == "Yes")){
//Code here
}

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