Multiple Files check extension is Pdf - javascript

How can we get multiple uploaded files has pdf if yes then enable Div
case 1: when I am trying to upload pdf file pdf checkbox is enabled
Case 2: upload docx or any other file checkbox gets hides
Case 3: When trying to upload different files such as txt, docx, pdf it is not showing checkbox
Please suggest how can we check extensions for multiple uploaded files and if there is pdf extension show checkbox div.
if (extn == 'pdf' || extn=='PDF') {
$('#<%=chkAddPdfPassword.ClientID%>').removeAttr('checked');
$("#chkPdf").show();
} else {
$("#chkPdf").hide();
$("#divPasswordField").hide();
}
Thanks in Advance

var files = $('#<%=uploadFile.ClientID %>')[0].files;
for (var i = 0; i < files.length; i++) {
var a = checkFileExtension(files[i].name);
if ((a == "pdf") || (typeof a !== "undefined")) {
console.log('check pdf');
$("#chkPdf").show();
break;
}
else {
$("#chkPdf").hide();
}
}
function checkFileExtension(file) {
var extension = file.substr((file.lastIndexOf('.') + 1));
var fileExtn;
switch (extension) {
case 'pdf':
console.log('was pdf');
fileExtn = 'pdf';
break;
default:
console.log('who knows');
fileExtn = 'default';
}
return fileExtn;
};

Solution :
var files = $('#<%=uploadFile.ClientID %>')[0].files;
for (var i = 0; i < files.length; i++) {
var a = openFile(files[i].name);
if ((a == "pdf") || (typeof a !== "undefined")) {
console.log('check pdf');
$("#chkPdf").show();
break;
}
else {
$("#chkPdf").hide();
$("#divPasswordField").hide();
}
}
function openFile(file) {
var extension = file.substr((file.lastIndexOf('.') + 1));
var fileExtn;
switch (extension) {
case 'jpg':
case 'png':
case 'txt':
console.log('was txt'); // There's was a typo in the example where
break; // the alert ended with pdf instead of gif.
case 'zip':
case 'docx':
console.log('was docx');
break;
case 'pdf':
console.log('was pdf');
fileExtn = 'pdf';
break;
default:
console.log('who knows');
fileExtn = 'default';
}
return fileExtn;
};

Related

Submit form via AJAX using plain Javascript without Jquery get checked radio button value

I do have HTML form submitting via AJAX using plain Javascript:
function XMLhttp(){
var formInputs = document.getElementById(formID).querySelectorAll("input, textarea");
var selectFormGebdat = formID;
var toCheckDatesFields = document.getElementsByClassName("checkDate");
for (var i = 0; i < toCheckDatesFields.length; i++) {
if(toCheckDatesFields.item(i).value != '') {
var returnValue = validatedate(toCheckDatesFields.item(i));
if(returnValue == false) {
return false;
}
}
}
var httpRequest = new XMLHttpRequest();
var formData = new FormData();
for( var i=0; i < formInputs.length; i++ ){
formData.append(formInputs[i].name, formInputs[i].value);
}
httpRequest.onreadystatechange = function(){
if ( this.readyState == 4 && this.status == 200 ) {
resultData = JSON.parse(this.responseText);
if(resultData.success == true) {
document.getElementById('resultok').className += ' show';
}
else {
document.getElementById('resulterror').className += ' show';
};
}
};
httpRequest.open(formMethod, formAction);
httpRequest.send(formData);
}
selectButton.onclick = function(){
XMLhttp();
}
selectForm.onsubmit = function(){
return false;
}
My problem is, that I have radio buttons and select-box. But the selected values are not submitted. F.ex.
<input type="radio" name="typ" value="anfrage"><input type="radio" name="typ" value="reservierung">
If anfrage is checked, the script is not submitting this value - there is always reservierung submitting.
Thanks for help!
Martin
I could solve this by:
var formData = new FormData();
if (!form || form.nodeName !== "FORM") {
return;
}
var i, j, q = [];
for (i = form.elements.length - 1; i >= 0; i = i - 1) {
if (form.elements[i].name === "") {
continue;
}
switch (form.elements[i].nodeName) {
case 'INPUT':
switch (form.elements[i].type) {
case 'text':
case 'hidden':
case 'password':
case 'button':
case 'reset':
case 'submit':
formData.append(form.elements[i].name, form.elements[i].value);
break;
case 'checkbox':
case 'radio':
if (form.elements[i].checked) {
formData.append(form.elements[i].name, form.elements[i].value);
}
break;
case 'file':
break;
}
break;
case 'TEXTAREA':
formData.append(form.elements[i].name, form.elements[i].value);
break;
case 'SELECT':
switch (form.elements[i].type) {
case 'select-one':
formData.append(form.elements[i].name, form.elements[i].value);
break;
case 'select-multiple':
for (j = form.elements[i].options.length - 1; j >= 0; j = j - 1) {
if (form.elements[i].options[j].selected) {
formData.append(form.elements[i].name, form.elements[i].value);
}
}
break;
}
break;
case 'BUTTON':
switch (form.elements[i].type) {
case 'reset':
case 'submit':
case 'button':
formData.append(form.elements[i].name, form.elements[i].value);
break;
}
break;
}
}

Case in Switch statement involving array with conditions

I have an array which is to be iterated and depending on the condition execute task
I have done this with if else and trying it out with Switch.And the condition is if (1 && 2) (then execute A) else if (1) (then execute B) else if (2) (then execute c) else if (none) (then execute D)
function showFiletRelateddata(selectedFilter) {
/*if (selectedFilter.length === 0) {
console.log("No data");
} else if (
selectedFilter.includes("Request") &&
selectedFilter.includes("Reservation")
) {
console.log("RequestReservation");
} else if (selectedFilter.includes("Request")) {
console.log("Request");
} else if (selectedFilter.includes("Reservation")) {
console.log("Reservation");
}*/
var filt = selectedFilter;
for (var i = 0; i < filt.length; i++) {
var supp = filt[i];
switch (supp) {
case "Request":
case "Reservation":
console.log("RequestReservation");
break;
case "Request":
console.log("Request");
break;
case "Reservation":
console.log("Reservation");
break;
default:
console.log("No data");
}
}
}
The if else is working fine however what correction needs to be made for Switch statement
For ref =
Javascript switch case with array or strings
function showFiletRelateddata(selectedFilter) {
var filt = selectedFilter;
var supp = ""
for (var i = 0; i < filt.length; i++) { //loop over length of array
supp = supp + filt[i]; // concat elements of array
}
switch (supp) {
case "RequestReservation": // if case contains one of the condition
case "ReservationRequest":
console.log("RequestReservation");
break;
case "Request":
console.log("Request");
break;
case "Reservation":
console.log("Reservation");
break;
default:
console.log("No data");
}
}
var a = ["Reservation", "Request"];
var b = ["Request","Reservation"];
var c = ["Reservation"];
var d = ["Request"];
showFiletRelateddata(a);
showFiletRelateddata(b);
showFiletRelateddata(c);
showFiletRelateddata(d);

After finding a word in html body, replace it with another one

i have this code that finds a word in the entire body of a html page
var word = "active",
queue = [document.body],
curr
;
while (curr = queue.pop()) {
if (!curr.textContent.match(word)) continue;
for (var i = 0; i < curr.childNodes.length; ++i) {
switch (curr.childNodes[i].nodeType) {
case Node.TEXT_NODE : // 3
if (curr.childNodes[i].textContent.match(word)) {
console.log("Found!");
console.log(curr);
// you might want to end your search here.
word.replace('active', 'test');
}
break;
case Node.ELEMENT_NODE : // 1
queue.push(curr.childNodes[i]);
break;
}
}
}
And what i'm trying to do is if active is found, replace it with something else but i have tried some ways and i wasnt able to do it. What i'm doing wrong?
This would give the result you are looking for (I decided to skip of 'SCRIPT' nodeName's so you do not accidentally modify any code:
let word = "active";
let queue = [document.body];
let curr;
while (curr = queue.pop()) {
if (!curr.textContent.match(word) || curr.nodeName === 'SCRIPT') continue;
for (var i = 0; i < curr.childNodes.length; ++i) {
if (curr.childNodes[i].nodeName === 'SCRIPT') continue;
switch (curr.childNodes[i].nodeType) {
case Node.TEXT_NODE: // 3
if (curr.childNodes[i].textContent.match(word)) {
console.log("Found!", `'${curr.childNodes[i].textContent}'`);
curr.childNodes[i].textContent = curr.childNodes[i].textContent.replace('active', 'test');
}
break;
case Node.ELEMENT_NODE: // 1
queue.push(curr.childNodes[i]);
break;
}
}
}
<div>asdf</div>
<span>active</span>
<div>asdf</div>
Hope this helps,

Uncaught ReferenceError: resize_cnt is not defined

I've got the following challenge on my website.
Uncaught ReferenceError: resize_cnt is not defined
at loop (main.js:204)
at init.js:201
I've got embedded code on my website that opens modal window and the error only occurs on pages that have that modal included. That embedded code is a designstudio plugin where customer can design their own space and I've added woocommerce functions that collects the samples that customer requested and add them to the woocommerce basket.
// this is the main loop, always running
function loop() {
if (ao.sys.resize_enable == 1) {
if (ao.sys.loading == 0) {
ao.sys.global_loader.deact();
} else {
ao.sys.global_loader.act();
}
refresh_pos_size();
if (typeof (appli_loop) == "function") {
appli_loop();
}
if (typeof (cust_loop) == "function") {
cust_loop();
}
if (typeof (ao.init.orientation_enable) != 'undefined'
&& ao.init.orientation_enable == 1) {
detect_orientation();
}
if (ao.sys.rendering == 0
&& ($(window).height() != ao.sys.current_h || $(window).width() != ao.sys.current_w)) {
ao.sys.current_w = $(window).width();
ao.sys.current_h = $(window).height();
resize_done = 1;
resize_cnt = 0;
if (current_room_id > -1) {
set_display();
}
} else {
resize_cnt++; /** here is where the error occurs **/
}
if ((resize_done == 1) && (resize_cnt > 10)
&& (ao.sys.resize_enable == 1)) {
if (ao.sys.comp_active == 1) {
// btt_left_comp_func();
}
if (typeof (appli_loop_after_resize) == "function") {
appli_loop_after_resize();
}
if (typeof (cust_loop_after_resize) == "function") {
cust_loop_after_resize();
}
resize_done = 0;
resize_cnt = 0;
if (editor_mode != 0) {
scale_to_view();
function init_app() {
ao.sys.init_cnt++;
// alert(ao.sys.init_cnt);
debug.log('init_app: ' + ao.sys.init_cnt + ' / 13');
switch (ao.sys.init_cnt) {
case 1:
load_user();
break;
case 2:
if (ao.cfg.phonegap == 1) {
if (localStorage.getItem("country_" + ao.cfg.app_version) != null) {
ao.sys.current_country = localStorage.getItem("country_"
+ ao.cfg.app_version);
ao.cfg.language = localStorage.getItem("language_"
+ ao.cfg.app_version);
}
checkConnection();
init_db();
} else {
init_app();
}
break;
case 3:
load_language();
break;
case 4:
if (typeof load_catlist == 'function') {
load_catlist();
} else {
init_app();
}
break;
case 5:
load_room_list();
break;
case 6:
if (typeof load_prodlist_egger_vds_online == 'function') {
load_prodlist_egger_vds_online();
} else {
if (typeof load_prodlist == 'function') {
load_prodlist();
} else {
init_app();
}
}
break;
case 7:
init_application();
break;
case 8:
if ((ao.cfg.phonegap == 1)
&& (localStorage.getItem("country_" + ao.cfg.app_version) == null)) {
debug.log('load_settings call');
load_settings();
} else {
init_app();
}
break;
case 9:
if (ao.cfg.phonegap == 1) {
if (localStorage.getItem("country_" + ao.cfg.app_version) != localStorage
.getItem("old_country_" + ao.cfg.app_version)) {
localStorage.setItem("old_country_" + ao.cfg.app_version,
ao.sys.current_country);
debug.log('init_db2 call');
init_db2();
} else {
init_app();
}
} else {
init_app();
}
break;
case 10:
init_startscreen();
break;
case 11:
if (typeof init_appli == 'function') {
init_appli();
} else {
init_app();
}
break;
case 12:
if (typeof init_cust == 'function') {
init_cust();
} else {
init_app();
}
break;
case 13:
ao.sys.loading = 0;
window.setInterval(function() {
loop();
}, 40);
if (ao.sys.phonegap == 1) {
if ((ao.sys.device_plattform == "ios")
&& (ao.sys.device_type == 'phone')) {
window.setTimeout("check_iphone_landscape()", 150);
}
}
if (ao.sys.phonegap == 1) {
if (localStorage.getItem("country") == null) {
load_settings();
}
}
break;
default:
break;
}
}
Any ideas, solutions?
Replace resize_cnt++; with
if(typeof resize_cnt == 'undefined') { var resize_cnt = 0; }
resize_cnt++;
That'll solve your immediate error but you will run into this error elsewhere in your code because you haven't actually declared resize_cnt; anywhere.
Edit: Now that I can see your code, just set var resize_cnt = 0; right at the start of your script outside of any functions to give it global scope.
For best practice I'd probably recommend a recursive call on loop(); where you pass in the resize count, place this right at the end of your function and controlled by a setTimeout there.

How to find a word within an href link in a div element using jquery

There is a div element that always contain one of the array words inside an a href element. Depending on what the word is, I need the web page to redirect to a different page. I'm not sure what I a missing.
http://jsfiddle.net/5Legahgn/5/
if ($('.container-fluid:contains("It looks something like this")').length > 0 || ($('.container-fluid:contains("Have you visited")').length > 0)) {
var uTest = ["Saggitarius", "Quentin", "Cinthia", "Maleria", "Pete", "Library", "wizard"];
var aTest = uTest.length;
var i = 0;
for (i; i < aTest; i++) {
if ($('.container-fluid:contains("'+uTest[i]+'")').length > 0) {
var mTest = uTest[i];
if (mTest != null || undefined) {
switch (mTest) {
case "Saggitarius":
window.location.href = 'https://subeta.net/quests.php/saggitarius';
break;
case "Quentin":
window.location.href = '...';
break;
case "Cinthia":
window.location.href = '...';
break;
case "Maleria":
window.location.href = '...';
break;
case "Pete":
alert("Pete");
break;
case "Library":
window.location.href = '...';
break;
case "wizard":
window.location.href = '...';
break;
case "Mori":
window.location.href = '...';
break;
}
} else {
alert("done");
}
}
}
}

Categories

Resources