How to check a varying range of indexes in JavaScript - javascript

I have an array that contains some data. Each block of data is separated by an empty line, and the size of the block varies. How would I go about checking each block to see if it contained the values I need. Here is my current code that handles this. (This doesn't work because it will always check 7 lines)
input = ["ecl:gry pid:860033327 eyr:2020 hcl:#fffffd",
"byr:1937 iyr:2017 cid:147 hgt:183cm",
"",
"iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884",
"hcl:#cfa07d byr:1929",
"",
"hcl:#ae17e1 iyr:2013",
"eyr:2024",
"ecl:brn pid:760753108 byr:1931",
"hgt:179cm",
"",
"hcl:#cfa07d eyr:2025 pid:166559648",
"iyr:2011 ecl:brn hgt:59in"];
var byr = false;
var iyr = false;
var eyr = false;
var hgt = false;
var hcl = false;
var ecl = false;
var pid = false;
var numberOfLines = 1;
if (input[arrayIndex+1].charAt(0) == ""){
numberOfLines = 1;
} else if (input[arrayIndex+2].charAt(0) == ""){
numberOfLines = 2;
} else if (input[arrayIndex+3].charAt(0) == ""){
numberOfLines = 3;
} else if (input[arrayIndex+3].charAt(0) == ""){
numberOfLines = 3;
} else if (input[arrayIndex+4].charAt(0) == ""){
numberOfLines = 4;
} else if (input[arrayIndex+5].charAt(0) == ""){
numberOfLines = 5;
} else if (input[arrayIndex+6].charAt(0) == ""){
numberOfLines = 6;
}
else if (input[arrayIndex+7].charAt(0) == ""){
numberOfLines = 7;
}
if (input[arrayIndex].includes("byr:") || input[arrayIndex + 1].includes("byr:") || input[arrayIndex + 2].includes("byr:") || input[arrayIndex + 3].includes("byr:") || input[arrayIndex + 4].includes("byr:") || input[arrayIndex + 5].includes("byr:") || input[arrayIndex + 6].includes("byr:")) {
byr = true;
} else { byr = false; }
if (input[arrayIndex].includes("iyr") || input[arrayIndex + 1].includes("iyr:") || input[arrayIndex + 2].includes("iyr:") || input[arrayIndex + 3].includes("iyr:") || input[arrayIndex + 4].includes("iyr:") || input[arrayIndex + 5].includes("iyr:") || input[arrayIndex + 6].includes("iyr:")) {
iyr = true;
} else { iyr = false; }
if (input[arrayIndex].includes("eyr:") || input[arrayIndex + 1].includes("eyr:") || input[arrayIndex + 2].includes("eyr:") || input[arrayIndex + 3].includes("eyr:") || input[arrayIndex + 4].includes("eyr:") || input[arrayIndex + 5].includes("eyr:") || input[arrayIndex + 6].includes("eyr:")) {
eyr = true;
} else { eyr = false; }
if (input[arrayIndex].includes("hgt:") || input[arrayIndex + 1].includes("hgt:") || input[arrayIndex + 2].includes("hgt:") || input[arrayIndex + 3].includes("hgt:") || input[arrayIndex + 4].includes("hgt:") || input[arrayIndex + 5].includes("hgt:") || input[arrayIndex + 6].includes("hgt:")) {
hgt = true;
} else { hgt = false; }
if (input[arrayIndex].includes("hcl:") || input[arrayIndex + 1].includes("hcl:") || input[arrayIndex + 2].includes("hcl:") || input[arrayIndex + 3].includes("hcl:") || input[arrayIndex + 4].includes("hcl:") || input[arrayIndex + 5].includes("hcl:") || input[arrayIndex + 6].includes("hcl:")) {
hcl = true;
} else { hcl = false; }
if (input[arrayIndex].includes("ecl:") || input[arrayIndex + 1].includes("ecl:") || input[arrayIndex + 2].includes("ecl:") || input[arrayIndex + 3].includes("ecl:") || input[arrayIndex + 4].includes("ecl:") || input[arrayIndex + 5].includes("ecl:") || input[arrayIndex + 6].includes("ecl:")) {
ecl = true;
} else { ecl = false; }
if (input[arrayIndex].includes("pid:") || input[arrayIndex + 1].includes("pid:") || input[arrayIndex + 2].includes("pid:") || input[arrayIndex + 3].includes("pid:") || input[arrayIndex + 4].includes("pid:") || input[arrayIndex + 5].includes("pid:") || input[arrayIndex + 6].includes("pid:")) {
pid = true;
} else { pid = false; }
if (byr && iyr && eyr && hgt && hcl && ecl && pid) {
console.log("True");
} else {
console.log("False");
}

Using reduce to create an object with all your fields might be easier:
const inputStr = `ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in`;
const input = inputStr.split('\n\n'); // Split on empty lines
const fields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'];
input.forEach(passport => {
const res = fields.reduce((res, field) => ({
...res, // Keep the previous fields
[field]: passport.includes(field) // Add the current one
}), {});
console.log(JSON.stringify(res)); // {"byr": true, "iyr": true, ...}
const allFieldsArePresent = Object.values(res).every(val => val);
console.log(allFieldsArePresent);
});
When writing code, always try to think "DRY" (Don't Repeat Yourself). If you find yourself repeating things over and over, it's a big code-smell, there is a 99.999% chance it could be written in a shorter, more maintainable way.

Related

File reader API gives no out put on AUDIO files

As a part of a before upload control, I check if added files actually match the filetype that their extension describes.
I do a filecheck using a hidden element. and if it fails, it gets removed after formdata append.
I check the files at the backend too, but I also want to check via File Reader process. All functions like createItems or readItems and so on, are working in all other cases, meaning that they return output of either null, true or false. While I get output from image and video files, audio files not at all. It should work.
The question is: Does the file reader read audio files differently compared to video and image files?
The following is only a tiny block of the actual code.
Now the question is: Why it doesn't return output on audio files?.
quest0 & quest1 are UI elements. Which is the element that I pull the files from.
reader is "new FileReader".
what.filecount is added while page init as readonly property.
readItems gives output on all kind off files,
However the UI html doesn't get updated if it is the file type is audio/?
for (var oD = 0; oD < what.files.length; oD++)
{
var pL = false;
if (typeof window.dofile === 'undefined' || !window.dofile) { return; }
if (file_s.exec(file) && !what.multiple) { pL = true; }
else
{
if (what.files.length > what.filecount)
{
alert('too many files added. only ' + what.filecount + ' are allowed.');
what.value = '';
if (quest0 != null)
{ quest0.innerHTML = ('too many files added. only ' + what.filecount + ' are allowed.'); }
return;
}
pL = true;
if ( !lastform.includes(what.form.id) ) { pL = false; }
}
if (what.files && what.files[oD] && pL == true)
{
if (file_prev != null)
{
if (file_prev.getAttribute('data-src') == '')
{file_prev.setAttribute('data-src', file_prev.src); }
file_prev.src = file_backsrc;
}
reader.onload = function(e)
{
init();
if (typeof file_prev === 'undefined')
{
if (quest1 != null) { quest1.innerHTML = 'Error occoured'; }
return;
}
if (file_p == 'img' || file_p == 'video' || file_p == 'audio')
{ file_prev.src = e.target.result; }
if (file_p == 'canvas') { /*not yet*/ }
kill.classList.remove('invisible');
if (quest1 != null) { quest1.innerHTML = 'file done'; }
}
reader.onerror = function(e)
{
e.preventDefault();
alert('There was an error with one of your file(s). Filename ' + what.files[oD].name + '');
what.value = '';
}
function waitfor(arr)
{
if (arr == null || arr.length == 0) { return; }
var aL = arr.length,
aL0 = arr.shift();
file_prev = _(aL0.file_prev);
lastfile = aL0.lastfile;
file_p = aL0.file_p;
reader.readAsDataURL(aL0.file);
aL0.kill.classList.add('invisible');
setTimeout(() => {
init();
waitfor(arr)
}, 1250);
if (quest1 != null)
{ quest1.innerHTML = 'processing ' + file_p; }
}
if (what.multiple)
{
var oA = 0;
lastfile = what.files[oD].name;
for (oA; oA < dK.children.length; oA++)
{
var oB = what.files[oD], oB0, oB1;
if (oB.type.includes('im')) { file_p = 'img'; }
if (oB.type.includes('au')) { file_p = 'audio'; }
if (oB.type.includes('vi')) { file_p = 'video'; }
if (lastfile != what.files[oD].name || lastfilearr.length <= what.filecount)
{
oB0 = dK.children[oA];
if (oB0.nodeName.toLowerCase() == file_p && what.form.querySelectorAll(file_p)[oD] != null)
{
if ( /*oB0.getAttribute('data-src')==''&&*/ !lastfilearr.includes(what.files[oD].name))
{
oB0 = what.form.querySelectorAll(file_p)[oD];
file_prev = oB0;
lastfilearr.push(what.files[oD].name);
oB0.setAttribute('data-src', lastfile);
oB1 = new Object({
'file_prev': file_prev.id,
'file_p': file_p,
'file': what.files[oD],
'kill': kill
});
stoargearr.push(oB1);
createtoast(['Filechecker', 'the ' + what.files[oD].name + ' is getting checked.', 3e3, true, true, ['bg-info', 'text-dark', 'rounded', 'px-2', 'mx-1']]);
createItems('upload', 's', what.files[oD].name, 'name:' + what.files[oD].name + ';nr:' + oD + ';filesize:' + what.files[oD].size + ';filesizehuman:' + Math.abs(what.files[oD].size / 1024 / 1024).toFixed(2) + ';lastmodified:' + new Date(what.files[oD].lastModified).toUTCString() + ';type:' + what.files[oD].type + ';');
}
}
}
}
if (stoargearr.length == what.files.length) { waitfor(stoargearr); }
else { reader.readAsDataURL(what.files[oD]); }
}
}
}
Does the "File reader API" react different on audio files as it does on video or image files?
Thanks for any help.
here it goes.
for (var oD = 0; oD < what.files.length; oD++) {
var pL = false;
if (typeof window.dofile === 'undefined' || !window.dofile) {
return;
};
if (file_s.exec(file) && !what.multiple) {
pL = true;
} else {
if (what.files.length > what.filecount) {
alert('too many files added. only ' + what.filecount + ' are allowed.');
what.value = '';
if (quest0 != null) {
quest0.innerHTML = 'too many files added. only ' + what.filecount + ' are allowed.';
};
return;
};
pL = true;
if (!lastform.includes(what.form.id)) {
pL = false;
};
};
if (what.files && what.files[oD] && pL == true) {
if (file_prev != null) {
if (file_prev.getAttribute('data-src') == '') {
file_prev.setAttribute('data-src', file_prev.src);
};
file_prev.src = file_backsrc;
};
reader.onload = function(e) {
init();
if (typeof file_prev === 'undefined') {
if (quest1 != null) {
quest1.innerHTML = 'Error occoured';
};
return;
};
if (file_p == 'img' || file_p == 'video' || file_p == 'audio') {
file_prev.src = e.target.result;
};
if (file_p == 'canvas') { /*not yet*/ };
kill.classList.remove('invisible');
if (quest1 != null) {
quest1.innerHTML = 'file done';
};
};
reader.onerror = function(e) {
e.preventDefault();
alert('There was an error with one of your file(s). Filename ' + what.files[oD].name + '');
what.value = '';
};
function waitfor(arr) {
if (arr == null || arr.length == 0) {
return;
};
var aL = arr.length,
aL0 = arr.shift();
file_prev = _(aL0.file_prev);
lastfile = aL0.lastfile;
file_p = aL0.file_p;
reader.readAsDataURL(aL0.file);
aL0.kill.classList.add('invisible');
setTimeout(() => {
init();
waitfor(arr)
}, 1250);
if (quest1 != null) {
quest1.innerHTML = 'processing ' + file_p;
};
};
if (what.multiple) {
var oA = 0;
lastfile = what.files[oD].name;
for (oA; oA < dK.children.length; oA++) {
var oB = what.files[oD],
oB0, oB1;
if (oB.type.includes('im')) {
file_p = 'img';
};
if (oB.type.includes('au')) {
file_p = 'audio';
};
if (oB.type.includes('vi')) {
file_p = 'video';
};
if (lastfile != what.files[oD].name || lastfilearr.length <= what.filecount) {
oB0 = dK.children[oA];
if (oB0.nodeName.toLowerCase() == file_p && what.form.querySelectorAll(file_p)[oD] != null) {
if ( /*oB0.getAttribute('data-src')==''&&*/ !lastfilearr.includes(what.files[oD].name)) {
oB0 = what.form.querySelectorAll(file_p)[oD];
file_prev = oB0;
lastfilearr.push(what.files[oD].name);
oB0.setAttribute('data-src', lastfile);
oB1 = new Object({
'file_prev': file_prev.id,
'file_p': file_p,
'file': what.files[oD],
'kill': kill
});
stoargearr.push(oB1);
createtoast(['Filechecker', 'the ' + what.files[oD].name + ' is getting checked.', 3e3, true, true, ['bg-info', 'text-dark', 'rounded', 'px-2', 'mx-1']]);
createItems('upload', 's', what.files[oD].name, 'name:' + what.files[oD].name + ';nr:' + oD + ';filesize:' + what.files[oD].size + ';filesizehuman:' + Math.abs(what.files[oD].size / 1024 / 1024).toFixed(2) + ';lastmodified:' + new Date(what.files[oD].lastModified).toUTCString() + ';type:' + what.files[oD].type + ';');
};
};
};
};
if (stoargearr.length == what.files.length) {
waitfor(stoargearr);
};
} else {
reader.readAsDataURL(what.files[oD]);
};
};
};```
if (quest1 != null) {
quest1.innerHTML = 'processing ' + file_p;
};
This block gives no output on audio files.

I am trying to make a content editable div that has syntax highlighting

I am trying to make a contenteditable div that has syntax highlighting, but when I type, the cursor is fixed and text flows backwards. Please help me. What did I do wrong? I tried changing the direction of text, that didn't work out well. Also, when I type < after typing a few chracters, it clears the whole div!
Any answer is highly appreciated.
Thanks in advance!
P.S. Below is the code
var div = document.getElementById("myDiv");
div.oninput = function () {
this.innerHTML = this.textContent.replace("&lt", "<");
text = document.getElementById("myDiv");
syntaxHighlight(text);
}
function syntaxHighlight(elmnt, mode) {
var lang = (mode || "html");
var elmntObj = (document.getElementById(elmnt) || elmnt);
var elmntTxt = elmntObj.innerHTML;
var tagcolor = "mediumblue";
var tagnamecolor = "brown";
var attributecolor = "red";
var attributevaluecolor = "mediumblue";
var commentcolor = "green";
var cssselectorcolor = "brown";
var csspropertycolor = "red";
var csspropertyvaluecolor = "mediumblue";
var cssdelimitercolor = "black";
var cssimportantcolor = "red";
var jscolor = "black";
var jskeywordcolor = "mediumblue";
var jsstringcolor = "brown";
var jsnumbercolor = "red";
var jspropertycolor = "black";
elmntObj.style.fontFamily = "Consolas,'Courier New', monospace";
if (!lang) {lang = "html"; }
if (lang == "html") {elmntTxt = htmlMode(elmntTxt);}
if (lang == "css") {elmntTxt = cssMode(elmntTxt);}
if (lang == "js") {elmntTxt = jsMode(elmntTxt);}
elmntObj.innerHTML = elmntTxt;
function extract(str, start, end, func, repl) {
var s, e, d = "", a = [];
while (str.search(start) > -1) {
s = str.search(start);
e = str.indexOf(end, s);
if (e == -1) {e = str.length;}
if (repl) {
a.push(func(str.substring(s, e + (end.length))));
str = str.substring(0, s) + repl + str.substr(e + (end.length));
} else {
d += str.substring(0, s);
d += func(str.substring(s, e + (end.length)));
str = str.substr(e + (end.length));
}
}
this.rest = d + str;
this.arr = a;
}
function htmlMode(txt) {
var rest = txt, done = "", php, comment, angular, startpos, endpos, note, i;
comment = new extract(rest, "<!--", "-->", commentMode, "W3HTMLCOMMENTPOS");
rest = comment.rest;
while (rest.indexOf("<") > -1) {
note = "";
startpos = rest.indexOf("<");
if (rest.substr(startpos, 9).toUpperCase() == "&LT;STYLE") {note = "css";}
if (rest.substr(startpos, 10).toUpperCase() == "&LT;SCRIPT") {note = "javascript";}
endpos = rest.indexOf(">", startpos);
if (endpos == -1) {endpos = rest.length;}
done += rest.substring(0, startpos);
done += tagMode(rest.substring(startpos, endpos + 4));
rest = rest.substr(endpos + 4);
if (note == "css") {
endpos = rest.indexOf("</style>");
if (endpos > -1) {
done += cssMode(rest.substring(0, endpos));
rest = rest.substr(endpos);
}
}
if (note == "javascript") {
endpos = rest.indexOf("</script>");
if (endpos > -1) {
done += jsMode(rest.substring(0, endpos));
rest = rest.substr(endpos);
}
}
}
rest = done + rest;
for (i = 0; i < comment.arr.length; i++) {
rest = rest.replace("W3HTMLCOMMENTPOS", comment.arr[i]);
}
return rest;
}
function tagMode(txt) {
var rest = txt, done = "", startpos, endpos, result;
while (rest.search(/(\s|<br>)/) > -1) {
startpos = rest.search(/(\s|<br>)/);
endpos = rest.indexOf(">");
if (endpos == -1) {endpos = rest.length;}
done += rest.substring(0, startpos);
done += attributeMode(rest.substring(startpos, endpos));
rest = rest.substr(endpos);
}
result = done + rest;
result = "<span style=color:" + tagcolor + "><</span>" + result.substring(4);
if (result.substr(result.length - 4, 4) == ">") {
result = result.substring(0, result.length - 4) + "<span style=color:" + tagcolor + ">></span>";
}
return "<span style=color:" + tagnamecolor + ">" + result + "</span>";
}
function attributeMode(txt) {
var rest = txt, done = "", startpos, endpos, singlefnuttpos, doublefnuttpos, spacepos;
while (rest.indexOf("=") > -1) {
endpos = -1;
startpos = rest.indexOf("=");
singlefnuttpos = rest.indexOf("'", startpos);
doublefnuttpos = rest.indexOf('"', startpos);
spacepos = rest.indexOf(" ", startpos + 2);
if (spacepos > -1 && (spacepos < singlefnuttpos || singlefnuttpos == -1) && (spacepos < doublefnuttpos || doublefnuttpos == -1)) {
endpos = rest.indexOf(" ", startpos);
} else if (doublefnuttpos > -1 && (doublefnuttpos < singlefnuttpos || singlefnuttpos == -1) && (doublefnuttpos < spacepos || spacepos == -1)) {
endpos = rest.indexOf('"', rest.indexOf('"', startpos) + 1);
} else if (singlefnuttpos > -1 && (singlefnuttpos < doublefnuttpos || doublefnuttpos == -1) && (singlefnuttpos < spacepos || spacepos == -1)) {
endpos = rest.indexOf("'", rest.indexOf("'", startpos) + 1);
}
if (!endpos || endpos == -1 || endpos < startpos) {endpos = rest.length;}
done += rest.substring(0, startpos);
done += attributeValueMode(rest.substring(startpos, endpos + 1));
rest = rest.substr(endpos + 1);
}
return "<span style=color:" + attributecolor + ">" + done + rest + "</span>";
}
function attributeValueMode(txt) {
return "<span style=color:" + attributevaluecolor + ">" + txt + "</span>";
}
function commentMode(txt) {
return "<span style=color:" + commentcolor + ">" + txt + "</span>";
}
function cssMode(txt) {
var rest = txt, done = "", s, e, comment, i, midz, c, cc;
comment = new extract(rest, /\/\*/, "*/", commentMode, "W3CSSCOMMENTPOS");
rest = comment.rest;
while (rest.search("{") > -1) {
s = rest.search("{");
midz = rest.substr(s + 1);
cc = 1;
c = 0;
for (i = 0; i < midz.length; i++) {
if (midz.substr(i, 1) == "{") {cc++; c++}
if (midz.substr(i, 1) == "}") {cc--;}
if (cc == 0) {break;}
}
if (cc != 0) {c = 0;}
e = s;
for (i = 0; i <= c; i++) {
e = rest.indexOf("}", e + 1);
}
if (e == -1) {e = rest.length;}
done += rest.substring(0, s + 1);
done += cssPropertyMode(rest.substring(s + 1, e));
rest = rest.substr(e);
}
rest = done + rest;
rest = rest.replace(/{/g, "<span style=color:" + cssdelimitercolor + ">{</span>");
rest = rest.replace(/}/g, "<span style=color:" + cssdelimitercolor + ">}</span>");
for (i = 0; i < comment.arr.length; i++) {
rest = rest.replace("W3CSSCOMMENTPOS", comment.arr[i]);
}
return "<span style=color:" + cssselectorcolor + ">" + rest + "</span>";
}
function cssPropertyMode(txt) {
var rest = txt, done = "", s, e, n, loop;
if (rest.indexOf("{") > -1 ) { return cssMode(rest); }
while (rest.search(":") > -1) {
s = rest.search(":");
loop = true;
n = s;
while (loop == true) {
loop = false;
e = rest.indexOf(";", n);
if (rest.substring(e - 5, e + 1) == " ") {
loop = true;
n = e + 1;
}
}
if (e == -1) {e = rest.length;}
done += rest.substring(0, s);
done += cssPropertyValueMode(rest.substring(s, e + 1));
rest = rest.substr(e + 1);
}
return "<span style=color:" + csspropertycolor + ">" + done + rest + "</span>";
}
function cssPropertyValueMode(txt) {
var rest = txt, done = "", s;
rest = "<span style=color:" + cssdelimitercolor + ">:</span>" + rest.substring(1);
while (rest.search(/!important/i) > -1) {
s = rest.search(/!important/i);
done += rest.substring(0, s);
done += cssImportantMode(rest.substring(s, s + 10));
rest = rest.substr(s + 10);
}
result = done + rest;
if (result.substr(result.length - 1, 1) == ";" && result.substr(result.length - 6, 6) != " " && result.substr(result.length - 4, 4) != "<" && result.substr(result.length - 4, 4) != ">" && result.substr(result.length - 5, 5) != "&") {
result = result.substring(0, result.length - 1) + "<span style=color:" + cssdelimitercolor + ">;</span>";
}
return "<span style=color:" + csspropertyvaluecolor + ">" + result + "</span>";
}
function cssImportantMode(txt) {
return "<span style=color:" + cssimportantcolor + ";font-weight:bold;>" + txt + "</span>";
}
function jsMode(txt) {
var rest = txt, done = "", esc = [], i, cc, tt = "", sfnuttpos, dfnuttpos, compos, comlinepos, keywordpos, numpos, mypos, dotpos, y;
for (i = 0; i < rest.length; i++) {
cc = rest.substr(i, 1);
if (cc == "\\") {
esc.push(rest.substr(i, 2));
cc = "W3JSESCAPE";
i++;
}
tt += cc;
}
rest = tt;
y = 1;
while (y == 1) {
sfnuttpos = getPos(rest, "'", "'", jsStringMode);
dfnuttpos = getPos(rest, '"', '"', jsStringMode);
compos = getPos(rest, /\/\*/, "*/", commentMode);
comlinepos = getPos(rest, /\/\//, "<br>", commentMode);
numpos = getNumPos(rest, jsNumberMode);
keywordpos = getKeywordPos("js", rest, jsKeywordMode);
dotpos = getDotPos(rest, jsPropertyMode);
if (Math.max(numpos[0], sfnuttpos[0], dfnuttpos[0], compos[0], comlinepos[0], keywordpos[0], dotpos[0]) == -1) {break;}
mypos = getMinPos(numpos, sfnuttpos, dfnuttpos, compos, comlinepos, keywordpos, dotpos);
if (mypos[0] == -1) {break;}
if (mypos[0] > -1) {
done += rest.substring(0, mypos[0]);
done += mypos[2](rest.substring(mypos[0], mypos[1]));
rest = rest.substr(mypos[1]);
}
}
rest = done + rest;
for (i = 0; i < esc.length; i++) {
rest = rest.replace("W3JSESCAPE", esc[i]);
}
return "<span style=color:" + jscolor + ">" + rest + "</span>";
}
function jsStringMode(txt) {
return "<span style=color:" + jsstringcolor + ">" + txt + "</span>";
}
function jsKeywordMode(txt) {
return "<span style=color:" + jskeywordcolor + ">" + txt + "</span>";
}
function jsNumberMode(txt) {
return "<span style=color:" + jsnumbercolor + ">" + txt + "</span>";
}
function jsPropertyMode(txt) {
return "<span style=color:" + jspropertycolor + ">" + txt + "</span>";
}
function getDotPos(txt, func) {
var x, i, j, s, e, arr = [".","<", " ", ";", "(", "+", ")", "[", "]", ",", "&", ":", "{", "}", "/" ,"-", "*", "|", "%"];
s = txt.indexOf(".");
if (s > -1) {
x = txt.substr(s + 1);
for (j = 0; j < x.length; j++) {
cc = x[j];
for (i = 0; i < arr.length; i++) {
if (cc.indexOf(arr[i]) > -1) {
e = j;
return [s + 1, e + s + 1, func];
}
}
}
}
return [-1, -1, func];
}
function getMinPos() {
var i, arr = [];
for (i = 0; i < arguments.length; i++) {
if (arguments[i][0] > -1) {
if (arr.length == 0 || arguments[i][0] < arr[0]) {arr = arguments[i];}
}
}
if (arr.length == 0) {arr = arguments[i];}
return arr;
}
function getKeywordPos(typ, txt, func) {
var words, i, pos, rpos = -1, rpos2 = -1, patt;
if (typ == "js") {
words = ["abstract","arguments","boolean","break","byte","case","catch","char","class","const","continue","debugger","default","delete",
"do","double","else","enum","eval","export","extends","false","final","finally","float","for","function","goto","if","implements","import",
"in","instanceof","int","interface","let","long","NaN","native","new","null","package","private","protected","public","return","short","static",
"super","switch","synchronized","this","throw","throws","transient","true","try","typeof","var","void","volatile","while","with","yield"];
}
for (i = 0; i < words.length; i++) {
pos = txt.indexOf(words[i]);
if (pos > -1) {
patt = /\W/g;
if (txt.substr(pos + words[i].length,1).match(patt) && txt.substr(pos - 1,1).match(patt)) {
if (pos > -1 && (rpos == -1 || pos < rpos)) {
rpos = pos;
rpos2 = rpos + words[i].length;
}
}
}
}
return [rpos, rpos2, func];
}
function getPos(txt, start, end, func) {
var s, e;
s = txt.search(start);
e = txt.indexOf(end, s + (end.length));
if (e == -1) {e = txt.length;}
return [s, e + (end.length), func];
}
function getNumPos(txt, func) {
var arr = ["<br>", " ", ";", "(", "+", ")", "[", "]", ",", "&", ":", "{", "}", "/" ,"-", "*", "|", "%", "="], i, j, c, startpos = 0, endpos, word;
for (i = 0; i < txt.length; i++) {
for (j = 0; j < arr.length; j++) {
c = txt.substr(i, arr[j].length);
if (c == arr[j]) {
if (c == "-" && (txt.substr(i - 1, 1) == "e" || txt.substr(i - 1, 1) == "E")) {
continue;
}
endpos = i;
if (startpos < endpos) {
word = txt.substring(startpos, endpos);
if (!isNaN(word)) {return [startpos, endpos, func];}
}
i += arr[j].length;
startpos = i;
i -= 1;
break;
}
}
}
return [-1, -1, func];
}
}
$('div[contenteditable]').keydown(function(e) {
if (e.keyCode === 13) {
document.execCommand('insertHTML', false, '<br><br>');
return false;
}
});
div {
width: 300px;
height: 300px;
border: red solid;
box-sizing: border-box;
padding: 20px;
overflow: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myDiv" contenteditable></div>
You're missing an ending brace for your syntaxHighlight function, you have this:
function syntaxHighlight(elmnt, mode) {
var lang = (mode || "html");
var elmntObj = (document.getElementById(elmnt) || elmnt);
var elmntTxt = elmntObj.innerHTML;
var tagcolor = "mediumblue";
var tagnamecolor = "brown";
var attributecolor = "red";
var attributevaluecolor = "mediumblue";
var commentcolor = "green";
var cssselectorcolor = "brown";
var csspropertycolor = "red";
var csspropertyvaluecolor = "mediumblue";
var cssdelimitercolor = "black";
var cssimportantcolor = "red";
var jscolor = "black";
var jskeywordcolor = "mediumblue";
var jsstringcolor = "brown";
var jsnumbercolor = "red";
var jspropertycolor = "black";
elmntObj.style.fontFamily = "Consolas,'Courier New', monospace";
if (!lang) {lang = "html"; }
if (lang == "html") {elmntTxt = htmlMode(elmntTxt);}
if (lang == "css") {elmntTxt = cssMode(elmntTxt);}
if (lang == "js") {elmntTxt = jsMode(elmntTxt);}
elmntObj.innerHTML = elmntTxt;
But you want this:
function syntaxHighlight(elmnt, mode) {
var lang = (mode || "html");
var elmntObj = (document.getElementById(elmnt) || elmnt);
var elmntTxt = elmntObj.innerHTML;
var tagcolor = "mediumblue";
var tagnamecolor = "brown";
var attributecolor = "red";
var attributevaluecolor = "mediumblue";
var commentcolor = "green";
var cssselectorcolor = "brown";
var csspropertycolor = "red";
var csspropertyvaluecolor = "mediumblue";
var cssdelimitercolor = "black";
var cssimportantcolor = "red";
var jscolor = "black";
var jskeywordcolor = "mediumblue";
var jsstringcolor = "brown";
var jsnumbercolor = "red";
var jspropertycolor = "black";
elmntObj.style.fontFamily = "Consolas,'Courier New', monospace";
if (!lang) {lang = "html"; }
if (lang == "html") {elmntTxt = htmlMode(elmntTxt);}
if (lang == "css") {elmntTxt = cssMode(elmntTxt);}
if (lang == "js") {elmntTxt = jsMode(elmntTxt);}
elmntObj.innerHTML = elmntTxt;
}

Creating an array that tells if a given number is smaller than 10

I am trying to make this work, I don't know what is wrong with it.
It used to work with only 3 variables, when I made it longer it stoped and now I cannot make it work even with only 3. Now it gives me "Undefined Undefined Undefined".
What I want is to crate an alarm in a way that it shows the name of whatever has gone under 10. The fields it is comparing from are calculation fields with the REPEATABLESUM() code.
var choice1 = '';
var choice2 = '';
var choice3 = '';
var choice4 = '';
var choice5 = '';
var choice6 = '';
var choice7 = '';
var choice8 = '';
var choice9 = '';
var choice10 = '';
var choice11 = '';
var choice12 = '';
var choice13 = '';
var choice14 = '';
var choice15 = '';
var choice16 = '';
var choice17 = '';
var choice18 = '';
var choice19 = '';
var choice20 = '';
var choice21 = '';
var choice22 = '';
var choice23 = '';
var choice24 = '';
if (NUM($stock_coliblue_24) < 10) {
choice1 = LABEL('coliblue');
}
if (NUM($stock_absorvent_pads) < 10) {
choice2 = LABEL('absorvent_pads');
}
if (NUM($stock_micro_filters) < 10) {
choice3 = LABEL('micro_filters');
}
if (NUM($stock_alkaphotalkalinity) < 10) {
choice4 = LABEL('alkaphot');
}
if (NUM($stock_ammonia) < 10) {
choice5 = LABEL('ammonia');
}
if (NUM($stock_calcicol_calcium_hardness) < 10) {
choice6 = LABEL('calcicol');
}
if (NUM($stock_chloridol_nacl) < 10) {
choice7 = LABEL('chloridol');
}
if (NUM($stock_free_chlorine_dpd1) < 10) {
choice8 = LABEL('chlorine_free_dpd1');
}
if (NUM($stock_total_chlorine_dpd3) < 10) {
choice9 = LABEL('chlorine_total_dpd2');
}
if (NUM($stock_free_copper_n1) < 10) {
choice10 = LABEL('copper_free_n1');
}
if (NUM($stock_total_copper_n2) < 10) {
choice11 = LABEL('copper_total_n2');
}
if (NUM($stock_fluoride) < 10) {
choice12 = LABEL('fluoride');
}
if (NUM($stock_hardicol_total_hardness) < 10) {
choice13 = LABEL('hardicol');
}
if (NUM($stock_iron_hr) < 10) {
choice14 = LABEL('iron_hr');
}
if (NUM($stock_iron_mr) < 10) {
choice15 = LABEL('iron_mr');
}
if (NUM($stock_magnecol_magnesium) < 10) {
choice16 = LABEL('magnecol');
}
if (NUM($stock_manganese) < 10) {
choice17 = LABEL('manganese');
}
if (NUM($stock_nitratest_nitrate) < 10) {
choice18 = LABEL('nitratest');
}
if (NUM($stock_nitricol_nitrite) < 10) {
choice19 = LABEL('nitricol');
}
if (NUM($stock_phosphate) < 10) {
choice20 = LABEL('phosphate');
}
if (NUM($stock_potassium) < 10) {
choice20 = LABEL('potassium');
}
if (NUM($stock_silica) < 10) {
choice21 = LABEL('silica');
}
if (NUM($stock_sulphate) < 10) {
choice22 = LABEL('sulphate');
}
if (NUM($stock_sulphitest_sulphite) < 10) {
choice23 = LABEL('sulphitest');
}
if (NUM($stock_zinc) < 10) {
choice24 = LABEL('zinc');
}
if (choice1 == null && choice2 == null && choice3 == null && choice4 == null && choice5 == null && choice6 == null && choice7 == null && choice8 == null && choice9 == null && choice10 == null && choice11 == null && choice12 == null && choice13 == null && choice14 == null && choice15 == null && choice16 == null && choice17 == null && choice18 == null && choice19 == null && choice20 == null && choice21 == null && choice22 == null && choice23 == null && choice24 == null) {
SETRESULT('');
} else {
CONCAT (choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8 + choice9 + choice10 + choice11 + choice12 + choice13 + choice14 + choice15 + choice16 + choice17 + choice18 + choice19 + choice20 + choice21 + choice22 + choice23 + choice24);
}
user array
var choice = [];
var concatStr = "";
if (NUM($stock_coliblue_24) < 10) {
choice[0] = 'coliblue';
}
if (NUM($stock_absorvent_pads) < 10) {
choice[1] = 'absorvent_pads';
}
if (choice.length == 0){
SETRESULT('');
} else {
concatStr = arr.join(" ");
}
var choice = []; /*Using array notation instead of using 24 separate variables*/
var concatStr = "";
if (NUM($stock_coliblue_24) < 10) {
choice[0] = 'coliblue'; /*storing value in array rather than in variables*/
}
,..
,..
if (NUM($stock_zinc) < 10) {
choice[23] = LABEL('zinc');
}
/* Using for loop to iterate through array to check for null values. This is easier than having '==' null check for all 24 variables*/
for(var i=0;i<24;i++) {
if(choice[i] == null) {
SETRESULT('');
break;
}
}
/*If none of the values are null, then concat the array elements. Array.join is so easier than concatenating 24 string variables.*/
if(choice.length==24) {
concatStr = choice.join(" ");
}
Hope this explains.

Why is win undefined?

I wrote this in javascript to chat. Now I can call openChat with clicking a button and that works. But if I call it from onmessage-event, I get the
error: Uncaught TypeError: Cannot read property 'document' of
undefined
I'm new to javascript and I don't know, why I get this error?. Can somebody maybe help me?
Thanks:)
Thats the code:
var app = new Vue({
el: '#app',
data: {
busy: false,
roomId: 'POWebinar',
joinedRoom: false,
chatWirCB: false,
chatKundenCB: false,
microphoneWirCB: false,
microphoneKundenCB: false,
user: 'Benutzer',
},
methods: {
openChat: function () {
console.log("in fkt openChat");
win = window.open('', 'myconsole',
'scrollbars=1, width=450,height=500'
+ ',menubar=1'
+ ',toolbar=1'
+', location=1'
+ ',resizable=1')
win.document.writeln("<head><meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'></head><body style='background-color:#ecf0f1'></body>");
chat = document.getElementById("chatHtml").innerHTML;
win.document.write(chat);
win.document.title = "PatOrg - Live Chat";
win.document.getElementById('input-text-chat').onkeyup = function (e) {
if (e.keyCode != 13) return;
// removing trailing/leading whitespace
this.value = this.value.replace(/^\s+|\s+$/g, '');
var a = new Date();
var b = a.getHours(); var c = a.getMinutes(); var d = a.getSeconds();
if (b < 10) { b = '0' + b; }
if (c < 10) { c = '0' + c; }
if (d < 10) { d = '0' + d; }
var time = b + ':' + c + ':' + d;
if (!this.value.length) return
console.log(this.value);
connection.send('<div id="chatSend" class="chat-OutputGET bubbleGET"> <font color="white">' + username + '(' + time + '): ' + this.value + '</font></div>');
console.log(username);
console.log('User (' + time + '): ' + this.value);
appendDIV('<div id="chatGet" class="chat-OutputSEND bubble"> <font color="white"> Ich (' + time + '): ' + this.value + '</font></div>');
this.value = '';
};
var chatContainer = win.document.getElementById('chat-output');
function appendDIV(event) {
if (event == "turnMicroOff" || event.data == "turnMicroOff" || event == "turnMicroOn" || event.data == "turnMicroOn" || event == "turnChatOn" || event.data == "turnChatOn" || event == "turnChatOff" || event.data == "turnChatOff") {
if (event == "turnMicroOff" || event.data == "turnMicroOff") {
console.log("audioID: " + audioID);
console.log("audioID: " + audioID);
for (i = 0; i < audioID.length; i++) {
connection.streamEvents[audioID[i]].stream.mute('audio');
}
}
else if (event == "turnMicroOn" || event.data == "turnMicroOn") {
console.log("audioID: " + audioID);
console.log("audioID: " + audioID);
for (i = 0; i < audioID.length; i++) {
connection.streamEvents[audioID[i]].stream.unmute('audio');
}
}
else if (event == "turnChatOn" || event.data == "turnChatOn") {
document.getElementById("btnChatOpen").style.visibility = "visible";
}
else if (event == "turnChatOff" || event.data == "turnChatOff") {
document.getElementById("btnChatOpen").style.visibility = "hidden";
}
}
else {
var div = document.createElement('div');
div.innerHTML = event.data || event;
chatContainer.appendChild(div);
div.tabIndex = 0;
div.focus();
win.document.getElementById('input-text-chat').focus();
emojify.run(win.document.getElementById('chat-output'));
}
}
connection.onmessage = appendDIV;
}
}
})
connection.onmessage = function (event) {
console.log(event.data);
if (event == "turnMicroOff" || event.data == "turnMicroOff") {
for (i = 0; i < audioID.length; i++) {
console.log("audioID: " + audioID[i]);
connection.streamEvents[audioID[i]].stream.mute('audio');
}
}
else if (event == "turnMicroOn" || event.data == "turnMicroOn") {
app.openChat();
for (i = 0; i < audioID.length; i++) {
console.log("audioID: " + audioID[i]);
connection.streamEvents[audioID[i]].stream.unmute('audio');
}
}
else if (event == "turnChatOn" || event.data == "turnChatOn") {
document.getElementById("btnChatOpen").style.visibility = "visible";
}
else if (event == "turnChatOff" || event.data == "turnChatOff") {
document.getElementById("btnChatOpen").style.visibility = "hidden";
}
}

Javascript required fields validation

I have a big form where I have to validate 44 fields. I'm using javascript form validation for requiring field input, and C# regular expressions for ensuring valid input. I won't go into details on why I had to do it this way, but that's how it works. I've triple checked this code block I have to validate the form and can't find the error. Code is below (large), hoping someone has better eyes than me.
C#
#using (Html.BeginForm("Create", "Table1_1", FormMethod.Post, new { onsubmit = "return validateit()", name = "myForm" }))
{
Javascript
<script>
function validateit() {
var x1 = document.forms["myForm"]["COMPANY1"].value;
var x2 = document.forms["myForm"]["STATE1"].value;
var x3 = document.forms["myForm"]["BILL_CODE1"].value;
var x4 = document.forms["myForm"]["EFFECTIVE_DATE1"].value;
var x5 = document.forms["myForm"]["RECORD_TYPE1"].value;
var x6 = document.forms["myForm"]["MASK_ID1"].value;
var x7 = document.forms["myForm"]["EXTENSION_ID1"].value;
var x8 = document.forms["myForm"]["OVERPAYMENT_LIMIT"].value;
var x9 = document.forms["myForm"]["UNDERPAYMENT_LIMIT"].value;
var x10 = document.forms["myForm"]["OVERPAYMENT_REFUND"].value;
var x11 = document.forms["myForm"]["PARTIAL_PAYMENT_REFUND"].value;
var x12 = document.forms["myForm"]["RETURN_PREMIUM_REFUND"].value;
var x13 = document.forms["myForm"]["CANCELLATION_REFUND"].value;
var x14 = document.forms["myForm"]["EFT_AUTH_CHECK"].value;
var x15 = document.forms["myForm"]["EFT_CHECK_BILL"].value;
var x16 = document.forms["myForm"]["EFT_NSF_LIMIT"].value;
var x17 = document.forms["myForm"]["DB_PREM_ROUND"].value;
var x18 = document.forms["myForm"]["CC_OPTION"].value;
var x19 = document.forms["myForm"]["NSF_CHECK_LIMIT"].value;
var x20 = document.forms["myForm"]["NSF_CHECK_OPTION"].value;
var x21 = document.forms["myForm"]["FIRST_TERM_TYPE"].value;
var x22 = document.forms["myForm"]["CARRY_DATE_OPTION"].value;
var x23 = document.forms["myForm"]["DB_DPAY_ENDORSE_DAYS"].value;
var x24 = document.forms["myForm"]["DATE_METHOD"].value;
var x25 = document.forms["myForm"]["RENEWAL_OPTION"].value;
var x26 = document.forms["myForm"]["DROP_DAYS"].value;
var x27 = document.forms["myForm"]["MULTI_PAY_IND"].value;
var x28 = document.forms["myForm"]["MINIMUM_PAYMENT"].value;
var x29 = document.forms["myForm"]["ENDORSEMENT_ACTION"].value;
var x30 = document.forms["myForm"]["I_AND_S_OPTION_DAYS"].value;
var x31 = document.forms["myForm"]["S_OPTION_PERCENT"].value;
var x32 = document.forms["myForm"]["SERVICE_CHARGE_OPTION"].value;
var x33 = document.forms["myForm"]["REINSTATE_OPTION"].value;
var x34 = document.forms["myForm"]["CASH_WITH_APP_OPTION"].value;
var x35 = document.forms["myForm"]["DB_CC_NOTICE"].value;
var x36 = document.forms["myForm"]["DOWN_PAY_RENEWAL_DAYS"].value;
var x37 = document.forms["myForm"]["MONTH_BY_TERM"].value;
var x38 = document.forms["myForm"]["LEAD_MONTHS"].value;
var x39 = document.forms["myForm"]["INITIAL_MONTHS"].value;
var x40 = document.forms["myForm"]["DB_CC_REJECTS"].value;
var x41 = document.forms["myForm"]["RETURN_ENDORSEMENT_OPTION"].value;
var x42 = document.forms["myForm"]["RTN_S_OPTION_PERCENT"].value;
var x43 = document.forms["myForm"]["DB_AUTO_REF_DAY"].value;
var x44 = document.forms["myForm"]["RENEW_OPTION_BILL_PLAN"].value;
var count = 0;
if (x1 == null || x1 == "" || x1 == " ") {
document.getElementById("demo1").innerHTML = "<font style='color:red';>Error: Company number is required</font>";
count = count + 1;
}
else {
document.getElementById("demo1").innerHTML = null;
}
if (x2 == null || x2 == "" || x2 == " ") {
document.getElementById("demo2").innerHTML = "<font style='color:red';>Error: State code is required</font>";
count = count + 1;
}
else {
document.getElementById("demo2").innerHTML = null;
}
if (x3 == null || x3 == "" || x3 == " ") {
document.getElementById("demo3").innerHTML = "<font style='color:red';>Error: Bill code is required</font>";
count = count + 1;
}
else {
document.getElementById("demo3").innerHTML = null;
}
if (x4 == null || x4 == "" || x4 == " ") {
document.getElementById("demo4").innerHTML = "<font style='color:red';>Error: Effective date is required</font>";
count = count + 1;
}
else {
document.getElementById("demo4").innerHTML = null;
}
if (x5 == null || x5 == "" || x5 == " ") {
document.getElementById("demo5").innerHTML = "<font style='color:red';>Error: Record type is required</font>";
count = count + 1;
}
else {
document.getElementById("demo5").innerHTML = null;
}
if (x6 == null || x6 == "" || x6 == " ") {
document.getElementById("demo6").innerHTML = "<font style='color:red';>Error: Mask id is required</font>";
count = count + 1;
}
else {
document.getElementById("demo6").innerHTML = null;
}
if (x7 == null || x7 == "" || x7 == " ") {
document.getElementById("demo7").innerHTML = "<font style='color:red';>Error: Extension id is required</font>";
count = count + 1;
}
else {
document.getElementById("demo7").innerHTML = null;
}
if (x8 == null || x8 == "" || x8 == " ") {
document.getElementById("demo8").innerHTML = "<font style='color:red';>Error: Overpayment limit is required</font>";
count = count + 1;
}
else {
document.getElementById("demo8").innerHTML = null;
}
if (x9 == null || x9 == "" || x9 == " ") {
document.getElementById("demo9").innerHTML = "<font style='color:red';>Error: Underpayment limit is required</font>";
count = count + 1;
}
else {
document.getElementById("demo9").innerHTML = null;
}
if (x10 == null || x10 == "" || x10 == " ") {
document.getElementById("demo10").innerHTML = "<font style='color:red';>Error: Overpayment refund is required</font>";
count = count + 1;
}
else {
document.getElementById("demo10").innerHTML = null;
}
if (x11 == null || x12 == "" || x11 == " ") {
document.getElementById("demo11").innerHTML = "<font style='color:red';>Error: Partial payment refund is required</font>";
count = count + 1;
}
else {
document.getElementById("demo11").innerHTML = null;
}
if (x12 == null || x12 == "" || x12 == " ") {
document.getElementById("demo12").innerHTML = "<font style='color:red';>Error: Return premium refund is required</font>";
count = count + 1;
}
else {
document.getElementById("demo12").innerHTML = null;
}
if (x13 == null || x13 == "" || x13 == " ") {
document.getElementById("demo13").innerHTML = "<font style='color:red';>Error: Cancellation refund is required</font>";
count = count + 1;
}
else {
document.getElementById("demo13").innerHTML = null;
}
if (x14 == null || x14 == "" || x14 == " ") {
document.getElementById("demo14").innerHTML = "<font style='color:red';>Error: EFT auth check</font>";
count = count + 1;
}
else {
document.getElementById("demo14").innerHTML = null;
}
if (x15 == null || x15 == "" || x15 == " ") {
document.getElementById("demo15").innerHTML = "<font style='color:red';>Error: EFT check bill is required</font>";
count = count + 1;
}
else {
document.getElementById("demo15").innerHTML = null;
}
if (x16 == null || x16 == "" || x16 == " ") {
document.getElementById("demo16").innerHTML = "<font style='color:red';>Error: EFT NSF limit is required</font>";
count = count + 1;
}
else {
document.getElementById("demo16").innerHTML = null;
}
if (x17 == null || x17 == "" || x17 == " ") {
document.getElementById("demo17").innerHTML = "<font style='color:red';>Error: Premium rounding is required</font>";
count = count + 1;
}
else {
document.getElementById("demo17").innerHTML = null;
}
if (x18 == null || x18 == "" || x18 == " ") {
document.getElementById("demo18").innerHTML = "<font style='color:red';>Error: Credit card option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo18").innerHTML = null;
}
if (x19 == null || x19 == "" || x19 == " ") {
document.getElementById("demo19").innerHTML = "<font style='color:red';>Error: NSF check limit is required</font>";
count = count + 1;
}
else {
document.getElementById("demo19").innerHTML = null;
}
if (x20 == null || x20 == "" || x20 == " ") {
document.getElementById("demo20").innerHTML = "<font style='color:red';>Error: NSF check option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo20").innerHTML = null;
}
if (x21 == null || x21 == "" || x21 == " ") {
document.getElementById("demo21").innerHTML = "<font style='color:red';>Error: First term type is required</font>";
count = count + 1;
}
else {
document.getElementById("demo21").innerHTML = null;
}
if (x22 == null || x22 == "" || x22 == " ") {
document.getElementById("demo22").innerHTML = "<font style='color:red';>Error: Carry date option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo22").innerHTML = null;
}
if (x23 == null || x23 == "" || x23 == " ") {
document.getElementById("demo23").innerHTML = "<font style='color:red';>Error: Down pay endorse days is required</font>";
count = count + 1;
}
else {
document.getElementById("demo23").innerHTML = null;
}
if (x24 == null || x24 == "" || x24 == " ") {
document.getElementById("demo24").innerHTML = "<font style='color:red';>Error: Date method is required</font>";
count = count + 1;
}
else {
document.getElementById("demo24").innerHTML = null;
}
if (x25 == null || x25 == "" || x25 == " ") {
document.getElementById("demo25").innerHTML = "<font style='color:red';>Error: Renewal option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo25").innerHTML = null;
}
if (x26 == null || x26 == "" || x26 == " ") {
document.getElementById("demo26").innerHTML = "<font style='color:red';>Error: Drop days is required</font>";
count = count + 1;
}
else {
document.getElementById("demo26").innerHTML = null;
}
if (x27 = null || x27 == "" || x27 == " ") {
document.getElementById("demo27").innerHTML = "<font style='color:red';>Error: Multipay ind is required</font>";
count = count + 1;
}
else {
document.getElementById("demo27").innerHTML = null;
}
if (x28 == null || x28 == "" || x28 == " ") {
document.getElementById("demo28").innerHTML = "<font style='color:red';>Error: Minimum payment is required</font>";
count = count + 1;
}
else {
document.getElementById("demo28").innerHTML = null;
}
if (x29 == null || x29 == "" || x29 == " ") {
document.getElementById("demo29").innerHTML = "<font style='color:red';>Error: Endorsement action is required</font>";
count = count + 1;
}
else {
document.getElementById("demo29").innerHTML = null;
}
if (x30 == null || x30 == "" || x30 == " ") {
document.getElementById("demo30").innerHTML = "<font style='color:red';>Error: I and S option days is required</font>";
count = count + 1;
}
else {
document.getElementById("demo30").innerHTML = null;
}
if (x31 == null || x31 == "" || x31 == " ") {
document.getElementById("demo31").innerHTML = "<font style='color:red';>Error: S option percent is required</font>";
count = count + 1;
}
else {
document.getElementById("demo31").innerHTML = null;
}
if (x32 == null || x32 == "" || x32 == " ") {
document.getElementById("demo32").innerHTML = "<font style='color:red';>Error: Service charge option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo32").innerHTML = null;
}
if (x33 == null || x33 == "" || x33 == " ") {
document.getElementById("demo33").innerHTML = "<font style='color:red';>Error: Reinstate option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo33").innerHTML = null;
}
if (x34 == null || x34 == "" || x34 == " ") {
document.getElementById("demo34").innerHTML = "<font style='color:red';>Error: Cash with application is required</font>";
count = count + 1;
}
else {
document.getElementById("demo34").innerHTML = null;
}
if (x35 == null || x35 == "" || x35 == " ") {
document.getElementById("demo35").innerHTML = "<font style='color:red';>Error: Credit card notice is required</font>";
count = count + 1;
}
else {
document.getElementById("demo35").innerHTML = null;
}
if (x36 == null || x36 == "" || x36 == " ") {
document.getElementById("demo36").innerHTML = "<font style='color:red';>Error: Down pay renewal days is required</font>";
count = count + 1;
}
else {
document.getElementById("demo36").innerHTML = null;
}
if (x37 == null || x37 == "" || x37 == " ") {
document.getElementById("demo37").innerHTML = "<font style='color:red';>Error: Month by term is required</font>";
count = count + 1;
}
else {
document.getElementById("demo37").innerHTML = null;
}
if (x38 == null || x38 == "" || x38 == " ") {
document.getElementById("demo38").innerHTML = "<font style='color:red';>Error: Lead months is required</font>";
count = count + 1;
}
else {
document.getElementById("demo38").innerHTML = null;
}
if (x39 == null || x39 == "" || x39 == " ") {
document.getElementById("demo39").innerHTML = "<font style='color:red';>Error: Initial months is required</font>";
count = count + 1;
}
else {
document.getElementById("demo39").innerHTML = null;
}
if (x40 == null || x40 == "" || x40 == " ") {
document.getElementById("demo40").innerHTML = "<font style='color:red';>Error: Credit card rejects is required</font>";
count = count + 1;
}
else {
document.getElementById("demo40").innerHTML = null;
}
if (x41 == null || x41 == "" || x41 == " ") {
document.getElementById("demo41").innerHTML = "<font style='color:red';>Error: Return endorsement option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo41").innerHTML = null;
}
if (x42 == null || x42 == "" || x42 == " ") {
document.getElementById("demo42").innerHTML = "<font style='color:red';>Error: Return S option percent is required</font>";
count = count + 1;
}
else {
document.getElementById("demo42").innerHTML = null;
}
if (x43 == null || x43 == "" || x43 == " ") {
document.getElementById("demo43").innerHTML = "<font style='color:red';>Error: Auto ref day is required</font>";
count = count + 1;
}
else {
document.getElementById("demo43").innerHTML = null;
}
if (x44 == null || x44 == "" || x44 == " ") {
document.getElementById("demo44").innerHTML = "<font style='color:red';>Error: Renew option bill plan is required</font>";
count = count + 1;
}
else {
document.getElementById("demo44").innerHTML = null;
}
if (count > 0) {
return false;
}
}
</script>
Note: this code works in all of my other views. I must have a typo somewhere but I just can't seem to find it. thanks!
The x27 check is assigning to null instead of testing for null:
if (x27 = null || x27 == "" || x27 == " ") {
Should be
if (x27 == null || x27 == "" || x27 == " ") {
Although you might find something like this easier to work with if you really do need this function:
function validateit() {
var fields = [
["COMPANY1", "Company number is required"],
["STATE1", "State code is required"],
["BILL_CODE1", "BILL_CODE1 is required"],
["EFFECTIVE_DATE1", "EFFECTIVE_DATE1 is required"],
["RECORD_TYPE1", "RECORD_TYPE1 is required"],
// ...
];
var count = 0;
for (var i = 0; i < fields.length; i++) {
var x = document.forms.myForm[fields[i][0]].value;
if (x === null || x === "" || x === " ") {
count++;
document.getElementById("demo" + (i + 1)).innerHTML = "<font style='color:red';>Error: " + fields[i][1] + "</font>";
}
else {
document.getElementById("demo" + (i + 1)).innerHTML = "";
}
}
}

Categories

Resources