If someone could point out what I'm doing wrong I'd be eternally grateful! I can't seem to get the right combination of parenthesis - how to I combine multiple conditions in one statement?? Obviously I don't expect anyone to modify the code below, I just want to show what I'm trying to achieve.
If someone can explain the logic to me that'd be great
Thanks
function ChangeButton()
{
if
((document.forms[0].IPR.value == "") && (document.forms[0].FNM1.value == "") && (document.forms[0].FNM1.value == "") && (document.forms[0].SURN.value == "") && (document.forms[0].GEND.value == "") && (document.forms[0].DOB.value == "") && (document.forms[0].CRIM.value == "") && (document.forms[0].ETHC.value == "") && (document.forms[0].DSBC.value == "") && (document.forms[0].MARK1.value == "") && (document.forms[0].NATC.value == "") && (document.forms[0].COBC.value == "") && (document.forms[0].COD.value == "") && (document.forms[0].FIVE.value == "") && (document.forms[0].PERM.value == "") && (document.forms[0].VISF.value == "") && (document.forms[0].USD.value == "") && (document.forms[0].HAD1.value == "") && (document.forms[0].HAD3.value == "") && (document.forms[0].HTEL.value == "") && (document.forms[0].HAEM.value == "") && (document.forms[0].FEES.value == "") && (document.forms[0].REF1TIT.value == "") && (document.forms[0].REF1ORG.value == "") && (document.forms[0].REF1POS.value == "") && (document.forms[0].REF1AL1.value == "") && (document.forms[0].REF1AL3.value == "") && (document.forms[0].REF1AL5.value == "") && (document.forms[0].REF1EMA.value == "") && (document.forms[0].DISC.value == ""))
&&
((document.forms[0].PERM.value == "") && (document.forms[0].FIVE.value == "N"))
&&
((document.forms[0].AGNT.value == "") && (document.forms[0].USD.value == "Y"))
&&
((document.forms[0].CSTRT.value == "") && (document.forms[0].USD.value == "N") && (document.forms[0].CENDD.value == "") && (document.forms[0].CAD1.value == "") && (document.forms[0].CAD3.value == "") && (document.forms[0].CAD4.value == "") && (document.forms[0].CAPC.value == "") && (document.forms[0].CTEL.value == ""))
&&
((document.forms[0].AWDB.value == "") && (document.forms[0].FEES.value == "") && (document.forms[0].FEES.value == "Private Funds Self or Family") && (document.forms[0].AWDS.value == ""))
&&
((document.forms[0].RESEARCH.value == "Y") && (document.forms[0].RESSRT.value == "") && (document.forms[0].RESMOA.value == "") && (document.forms[0].RESAR.value == "") && (document.forms[0].RESDIS.value == ""))
{
document.getElementById('submitbutton').className = 'enabled';
}
else {
document.getElementById('submitbutton').className = 'disabled';
}
}
I see
...&&... document.forms[0].FIVE.value == ""
...&&... document.forms[0].FIVE.value == "N"
This never be true
EDIT
I think you must change approach, try something like this:
function ChangeButton()
{
var frm = document.forms[0];
var neverEmpty = ['field1','field2','field3'];
var mustBe = {field3:'Y', field4:'N'};
var status = 'ok';
for(var i = 0; i<neverEmpty.length; i++) {
if(frm[neverEmpty[i]] == '') {
status = 'ko';
break;
}
}
for(myField in mustBe) {
if(frm[myfield] != mustBe[myField]) {
status = 'ko';
break;
}
}
document.getElementById('submitbutton').className = status=='ok'? 'enabled' : 'disabled';
}
you don't close the brackets
if (document.forms[0].IPR.value == "" && document.forms[0].FNM1.value == "" && ect...)
it's that simple
You need one more set of parentheses around the whole lot i.e. if (a == b) { .. }
As far as I can see, you don't need any parentheses here (except for those that are required by if syntax).
if(document.forms[0].IPR.value == "" && document.forms[0].FNM1.value == "" &&
document.forms[0].PERM.value == "" && document.forms[0].FIVE.value == "N" &&
...
) {
document.getElementById('submitbutton').className = 'enabled';
} else {
document.getElementById('submitbutton').className = 'disabled';
}
Give the input elements that must be non-empty a "class" attribute. Then find all those elements using that instead of writing that insanely ugly code.
You need 1 more paren before and before the first curly brace everything if ( ... ) { ... }
Here is the corrected code.
function ChangeButton()
{
if
((document.forms[0].IPR.value == "") && (document.forms[0].FNM1.value == "") && (document.forms[0].FNM1.value == "") && (document.forms[0].SURN.value == "") && (document.forms[0].GEND.value == "") && (document.forms[0].DOB.value == "") && (document.forms[0].CRIM.value == "") && (document.forms[0].ETHC.value == "") && (document.forms[0].DSBC.value == "") && (document.forms[0].MARK1.value == "") && (document.forms[0].NATC.value == "") && (document.forms[0].COBC.value == "") && (document.forms[0].COD.value == "") && (document.forms[0].FIVE.value == "") && (document.forms[0].PERM.value == "") && (document.forms[0].VISF.value == "") && (document.forms[0].USD.value == "") && (document.forms[0].HAD1.value == "") && (document.forms[0].HAD3.value == "") && (document.forms[0].HTEL.value == "") && (document.forms[0].HAEM.value == "") && (document.forms[0].FEES.value == "") && (document.forms[0].REF1TIT.value == "") && (document.forms[0].REF1ORG.value == "") && (document.forms[0].REF1POS.value == "") && (document.forms[0].REF1AL1.value == "") && (document.forms[0].REF1AL3.value == "") && (document.forms[0].REF1AL5.value == "") && (document.forms[0].REF1EMA.value == "") && (document.forms[0].DISC.value == "")
&&
((document.forms[0].PERM.value == "") && (document.forms[0].FIVE.value == "N"))
&&
((document.forms[0].AGNT.value == "") && (document.forms[0].USD.value == "Y"))
&&
((document.forms[0].CSTRT.value == "") && (document.forms[0].USD.value == "N") && (document.forms[0].CENDD.value == "") && (document.forms[0].CAD1.value == "") && (document.forms[0].CAD3.value == "") && (document.forms[0].CAD4.value == "") && (document.forms[0].CAPC.value == "") && (document.forms[0].CTEL.value == ""))
&&
((document.forms[0].AWDB.value == "") && (document.forms[0].FEES.value == "") && (document.forms[0].FEES.value == "Private Funds Self or Family") && (document.forms[0].AWDS.value == ""))
&&
((document.forms[0].RESEARCH.value == "Y") && (document.forms[0].RESSRT.value == "") && (document.forms[0].RESMOA.value == "") && (document.forms[0].RESAR.value == "") && (document.forms[0].RESDIS.value == "")))
{
document.getElementById('submitbutton').className = 'enabled';
}
else {
document.getElementById('submitbutton').className = 'disabled';
}
}
USE and IDE, it will make ur life simple.. Cheers to Eclipse IDE :)
Related
So I have this if statement and I want to make all the possible outcomes.
if (cuisine == '' && area == '' && city == '' && minprice == '' && maxprice == ''){}
if (cuisine != '' && area == '' && city == '' && minprice == '' && maxprice == ''){}
if (cuisine != '' && area != '' && city == '' && minprice == '' && maxprice == ''){}
if (cuisine != '' && area != '' && city != '' && minprice == '' && maxprice == ''){}
if (cuisine != '' && area != '' && city != '' && minprice != '' && maxprice == ''){}
if (cuisine != '' && area != '' && city != '' && minprice != '' && maxprice != ''){}
etc... etc...
Here is a coding version of the truth table
const conditions = {
"cuisine": '',
"area": '',
"city": '',
"minprice": '',
"maxprice": ''
}
const arr = Object.keys(conditions),
n = arr.length,
m = 1 << n,
table = Array.from({length: m}).map((_,i) => {
let s = i.toString(2); // convert to binary
let len = n + 1 - s.length
s = Array.from({length:len}).join('0') + s; // pad with zeroes
return `if (${s.split('').map((bit,j) => `${arr[j]}${bit==="1"?' == ':' != '}''`).join(' && ')}) {...}`;
})
document.querySelector('pre').innerHTML = ` ${table.sort().join('\nelse ')}`
<pre></pre>
These are the all the possible outcomes
if(cuisine == '' && area == '' && city == '' && minprice == '' && maxprice == ''){}
else if (cuisine == '' && area == '' && city == '' && minprice == '' && maxprice !== ''){}
else if (cuisine == '' && area == '' && city == '' && minprice !== '' && maxprice == ''){}
else if (cuisine == '' && area == '' && city == '' && minprice !== '' && maxprice !== ''){}
else if (cuisine == '' && area == '' && city !== '' && minprice == '' && maxprice == ''){}
else if (cuisine == '' && area == '' && city !== '' && minprice == '' && maxprice !== ''){}
else if (cuisine == '' && area == '' && city !== '' && minprice !== '' && maxprice == ''){}
else if (cuisine == '' && area == '' && city !== '' && minprice !== '' && maxprice !== ''){}
else if (cuisine == '' && area !== '' && city == '' && minprice == '' && maxprice == ''){}
else if (cuisine == '' && area !== '' && city == '' && minprice == '' && maxprice !== ''){}
else if (cuisine == '' && area !== '' && city == '' && minprice !== '' && maxprice == ''){}
else if (cuisine == '' && area !== '' && city == '' && minprice !== '' && maxprice !== ''){}
else if (cuisine == '' && area !== '' && city !== '' && minprice == '' && maxprice == ''){}
else if (cuisine == '' && area !== '' && city !== '' && minprice == '' && maxprice !== ''){}
else if (cuisine == '' && area !== '' && city !== '' && minprice !== '' && maxprice == ''){}
else if (cuisine == '' && area !== '' && city !== '' && minprice !== '' && maxprice !== ''){}
else if (cuisine !== '' && area == '' && city == '' && minprice == '' && maxprice == ''){}
else if (cuisine !== '' && area == '' && city == '' && minprice == '' && maxprice !== ''){}
else if (cuisine !== '' && area == '' && city == '' && minprice !== '' && maxprice == ''){}
else if (cuisine !== '' && area == '' && city == '' && minprice !== '' && maxprice !== ''){}
else if (cuisine !== '' && area == '' && city !== '' && minprice == '' && maxprice == ''){}
else if (cuisine !== '' && area == '' && city !== '' && minprice == '' && maxprice !== ''){}
else if (cuisine !== '' && area == '' && city !== '' && minprice !== '' && maxprice == ''){}
else if (cuisine !== '' && area == '' && city !== '' && minprice !== '' && maxprice !== ''){}
else if (cuisine !== '' && area !== '' && city == '' && minprice == '' && maxprice == ''){}
else if (cuisine !== '' && area !== '' && city == '' && minprice == '' && maxprice !== ''){}
else if (cuisine !== '' && area !== '' && city == '' && minprice !== '' && maxprice == ''){}
else if (cuisine !== '' && area !== '' && city == '' && minprice !== '' && maxprice !== ''){}
else if (cuisine !== '' && area !== '' && city !== '' && minprice == '' && maxprice == ''){}
else if (cuisine !== '' && area !== '' && city !== '' && minprice == '' && maxprice !== ''){}
else if (cuisine !== '' && area !== '' && city !== '' && minprice !== '' && maxprice == ''){}
else if (cuisine !== '' && area !== '' && city !== '' && minprice !== '' && maxprice !== ''){}
There are 5 varing things that have two possible outcomes(empty or not empty)
Therefore there are 2x2x2x2x2 possibilities.
I have the following data:
I use this javascript:
var primary_phone ;
if (inter1.length == 10 && inter2.length == 10 && inter3.length == 0) {
primary_phone = inter1;
}
else if (inter1.length == 10 && inter2.length == 10 && inter3.length == 10) {
primary_phone = inter1;
}
else if (inter1.length != 10 && inter2.length == 10 && inter3.length == 0) {
primary_phone = inter2;
}
else if (inter1.length != 10 && inter2.length != 10 && inter3.length != 10) {
primary_phone = "+000000000000";
}
else if (inter1.length == 10 && inter2.length == 0 && inter3.length == 0) {
primary_phone = inter1;
}
And what I get is:
Instead of:
Do you have an idea?
Simplified:
var primary_phone;
if ( inter1.length == 10 && (inter3.length == 0 || inter3.length == 10) {
primary_phone = inter1;
} else if ( inter2.length == 10 && inter3.length == 0 ) {
primary_phone = inter2;
} else if ( inter3.length != 10 ) {
primary_phone = "+000000000000";
}
if ( primary_phone != undefined ) {
//primary_phone was assigned a value
}
The Javascript is not initialized per row, it (and the variables you assign) persists for the runtime of the transformation. In fact, you can have separate tabs for the start, per-row and end scripts.
To make it work, you need to set reset primary_phone either at the start of the script or in the else clause:
else {
primary_phone = null;
}
I feel like this is a really dumb question but my brain is fried. Right now I'm working on a very complex set of if and else statements and I'm wondering if there is an easier way? I originally tried or statements but that didn't give me the desired result.
if(fields[0].value === '' && fields[1].value === '' && fields[2].value === '' && fields[3].value === '' ) {
clusterGroup.addLayer(layer);
} else if(fields[0].value === '' && fields[1].value === city && fields[2].value === '' && fields[3].value === '') {
clusterGroup.addLayer(layer);
} else if(fields[0].value === state && fields[1].value === '' && fields[2].value === '' && fields[3].value === '') {
clusterGroup.addLayer(layer);
} else if(fields[0].value === state && fields[1].value === city && fields[2].value === '' && fields[3].value === ''){
clusterGroup.addLayer(layer);
} else if(fields[0].value === '' && fields[1].value === '' && (fields[2].value <= sq && sq != null) && fields[3].value === '') {
clusterGroup.addLayer(layer);
} else if(fields[0].value === '' && fields[1].value === '' && fields[2].value === '' && (fields[3].value >= sq && sq != null)) {
clusterGroup.addLayer(layer);
} else if(fields[0].value === '' && fields[1].value === city && (fields[2].value <= sq && sq != null) && fields[3].value === ''){
clusterGroup.addLayer(layer);
} else if(fields[0].value === state && fields[1].value === '' && fields[2].value === '' && (fields[3].value >= sq && sq != null)){
clusterGroup.addLayer(layer);
} else if(fields[0].value === state && fields[1].value === city && (fields[2].value <= sq && sq != null) && fields[3].value === ''){
clusterGroup.addLayer(layer);
} else if(fields[0].value === state && fields[1].value === city && fields[2].value === '' && (fields[3].value >= sq && sq != null)){
clusterGroup.addLayer(layer);
} else if(fields[0].value === state && fields[1].value === city && (fields[2].value <= sq && sq != null) && (fields[3].value >= sq && sq != null)){
clusterGroup.addLayer(layer);
}
Thank you in advanced.
I don't know how did you use OR Statement (||) but you can use one if statement.
if(
(fields[0].value === '' && fields[1].value === '' && fields[2].value === '' && fields[3].value === '') ||
(fields[0].value === '' && fields[1].value === city && fields[2].value === '' && fields[3].value === '') ||
(fields[0].value === state && fields[1].value === '' && fields[2].value === '' && fields[3].value === '') ||
(fields[0].value === state && fields[1].value === city && fields[2].value === '' && fields[3].value === '') ||
(fields[0].value === '' && fields[1].value === '' && (fields[2].value <= sq && sq != null) && fields[3].value === '') ||
(fields[0].value === '' && fields[1].value === '' && fields[2].value === '' && (fields[3].value >= sq && sq != null)) ||
(fields[0].value === '' && fields[1].value === city && (fields[2].value <= sq && sq != null) && fields[3].value === '') ||
(fields[0].value === state && fields[1].value === '' && fields[2].value === '' && (fields[3].value >= sq && sq != null)) ||
(fields[0].value === state && fields[1].value === city && (fields[2].value <= sq && sq != null) && fields[3].value === '') ||
(fields[0].value === state && fields[1].value === city && fields[2].value === '' && (fields[3].value >= sq && sq != null)) ||
(fields[0].value === state && fields[1].value === city && (fields[2].value <= sq && sq != null) && (fields[3].value >= sq && sq != null))
){
clusterGroup.addLayer(layer);
}
Try restructuring it into something like this
if (sq != null) {
if (fields[3].value >= sq &&
fields[2].value <= sq &&
fields[0].value === state &&
field[1].value === city) {
clusterGroup.addLayer(layer);
} else if (fields[3].value >= sq) {
if ((fields[0].value === state) ||
(fields[0].value === state && field[1].value === city) ||
(fields[0].value === '' &&
field[1].value === '')) {
clusterGroup.addLayer(layer);
}
} else if (fields[2].value <= sq) {
if ((fields[1].value === city) ||
(fields[0].value === state && field[1].value === city) ||
(fields[0].value === '' && field[1].value === '')) {
clusterGroup.addLayer(layer);
}
}
} else if ((fields[1].value === '' && fields[0].value === state) ||
(fields[0].value === '' && fields[1].value === city) ||
(fields[0].value === state && fields[1].value === city)) {
clusterGroup.addLayer(layer);
}
Validation doesn't work correctly. when we fill at any one field of these, it goes to function call
if ((name == "") && (uname == "") && (password1 == "") && (password2 == "") && (imei == "")) {
alert("Enter Necessary Fileld");
} else if (password1 == password2) {
registerDetails(name, uname, password1, managerId, address1, address2, city, state, country, postcode, number, email, imei, simCard);
} else {
alert("Password Doesn't match");
}
I guess, you wanted to use || instead of &&.
|| would mean, that any field must be empty to not validate, while && means what all fields must be empty.
Try this.
if ((name == "") || (uname == "") || (password1 == "") || (password2 == "") || (imei == "")) {
alert("Enter Necessary Fileld");
} else if (password1 == password2) {
registerDetails(name, uname, password1, managerId, address1, address2, city, state, country, postcode, number, email, imei, simCard);
} else {
alert("Password Doesn't match");
}
Change
if ((name == "") && (uname == "") && (password1 == "") && (password2 == "") && (imei == "")) {
alert("Enter Necessary Fileld");
}
to this so that it will be executed if anyone of the fields are null.
if ((name == "") || (uname == "") || (password1 == "") || (password2 == "") || (imei == "")) {
alert("Enter Necessary Fileld");
}
&& stands for conjunction, so only when all conditions are true, conjunction is true. Because your conditions check if elements are empty, in fact, you check if no one of these variables are empty.
Change && into ||, which stands for disjunction.
I have a HTML form with a file upload option where I do a quick validation of the file format on client side (in order to allow only certain file extensions).
The following code snippet works fine for me but I was wondering if there is a better or faster way to achieve the same, esp. if there are more extensions to be allowed in the future.
Note: This is only about the part with the multiple OR statements to check the file extension.
My code so far (working):
if( ( (fileNameShort.length <= 100) && (fileNameShort.indexOf('#') == -1) ) && ( (fileFormat == 'bmp') || (fileFormat == 'doc') || (fileFormat == 'docx') || (fileFormat == 'gif') || (fileFormat == 'jpeg') || (fileFormat == 'jpg') || (fileFormat == 'msg') || (fileFormat == 'png') || (fileFormat == 'pdf') ) )
Many thanks for any suggestions on this, Tim.
Use .indexOf()
and also use .toLowerCase() as checking for lowercase file formats
var arr=['bmp','doc','docx','gif','jpg','msg']; //create array filetypes
if(fileNameShort.length <= 100 && fileNameShort.indexOf('#') === -1 && arr.indexOf(fileFormat.toLowerCase()) !== -1)
You are using way too may parentheses.
if (
(
( fileNameShort.length <= 100 )
&& ( fileNameShort.indexOf('#') == -1 )
)
&&
(
(fileFormat == 'bmp') || (fileFormat == 'doc') || (fileFormat == 'docx') || (fileFormat == 'gif') || (fileFormat == 'jpeg') || (fileFormat == 'jpg') || (fileFormat == 'msg') || (fileFormat == 'png') || (fileFormat == 'pdf')
)
)
is equivalent to
if (
fileNameShort.length <= 100
&& fileNameShort.indexOf('#') == -1
&& (
fileFormat == 'bmp' || fileFormat == 'doc' || fileFormat == 'docx' || fileFormat == 'gif' || fileFormat == 'jpeg' || fileFormat == 'jpg' || fileFormat == 'msg' || fileFormat == 'png' || fileFormat == 'pdf'
)
)
is equivalent to
if (
fileNameShort.length <= 100
&& fileNameShort.indexOf('#') == -1
&& /^(bmp|docx?|gif|jpe?g|msg|png|pdf)$/i.test(fileFormat)
)