jQuery validation for multiple file upload

Please find below code It’s allowed only png, jpg, jpeg and pdf files and upload upto 3 files and not more than 10MB
jQuery(‘input[name=”file[]”]’).change(function(){
var tot_size=0;
var count_files = jQuery(this).get(0).files.length;
for (var i = 0; i < jQuery(this).get(0).files.length; ++i) {
var file_size=jQuery(this).get(0).files[i].size;
tot_size = tot_size+file_size;
}
for (var i = 0; i < jQuery(this).get(0).files.length; ++i) {
var file1=jQuery(this).get(0).files[i].name;
if(!/(\.png|\.jpg|\.jpeg|\.pdf)$/i.test(file1))
{
jQuery(‘input[type=”submit”]’).attr(‘disabled’,’disabled’);
jQuery(“.file_error”).text(“”);
jQuery(this).css(“border”, “1px solid #c00”);
var err_var=true;
}else{
jQuery(“.file_error”).text(“Only png, jpg, jpeg and pdf files are allowed (Upload upto 3 files and 10MB).”);
jQuery(‘input[type=”submit”]’).removeAttr(‘disabled’,’disabled’);
jQuery(this).css(“border”, “1px solid #ddd”);
var err_var=false;
}
}
if(err_var==false){
if(tot_size < 10485760 && count_files < 4){
jQuery(“.file_error”).text(“Only png, jpg, jpeg and pdf files are allowed (Upload upto 3 files and 10MB).”);
jQuery(‘input[type=”submit”]’).removeAttr(‘disabled’,’disabled’);
jQuery(this).css(“border”, “1px solid #ddd”);
}else{
jQuery(‘input[type=”submit”]’).attr(‘disabled’,’disabled’);
jQuery(“.file_error”).text(“”);
jQuery(this).css(“border”, “1px solid #c00”);
}
}
});

Advertisement

Smooth Scroll using jQuery

Please find below code for smooth scroll

$(document).ready(function(){
$(“a”).on(‘click’, function(event) {
if (this.hash !== “”) {
event.preventDefault();
var hash = this.hash;
$(‘html, body’).animate({
scrollTop: $(hash).offset().top
}, 1800, function(){
window.location.hash = hash;
});
}
});
});

Ajax call in wordpress functions.php using jQuery

/* Paste below code in header.php or footer.php */

jQuery.ajax({
type: “POST”,
url: “<?php echo admin_url( ‘admin-ajax.php’ ); ?>”,
data:{
action: “my_action”,
“date”: date,
“month”: month,
“year”: year
},
}).success(function(result){
alert(result);
});

/* Paste below code in functions.php */

add_action( ‘wp_ajax_my_action’, ‘my_action_callback’ );
add_action( ‘wp_ajax_nopriv_my_action’, ‘my_action_callback’ );

function my_action_callback(){
global $wpdb;
$events = $wpdb->get_results(“SELECT ID, title FROM table_name”);
$data = “<ul>”;
foreach($events as $evt){
$data .= “<li>”;
$data .= ‘<a href=”#” target=”_blank”>’.$evt->title.'</a>’;
$data .= “</li>”;
}
$data .= “</ul>”;
echo $data; exit;
}

Hide & Refresh iframe using jQuery

/* Hide iframe using jQuery */
jQuery(‘#iframe’, window.parent.document).hide();
/* Refresh iframe using jQuery */
jQuery(‘#iframe’,window.parent.document).attr(‘src’,jQuery(‘#iframe’,window.parent.document).attr(‘src’));

window.location in javascript

Please find below code…

//Get current page url
var curr_url = window.location.href

//Get hostname
var host_name = window.location.hostname

//Display the path name of the current URL
var path_name = window.location.pathname

//Get protocol of URL return http:// or https://
var proto = window.location.protocol;

//Get parameters from URL
var x = location.search;
Result:
?email=someone@example.com

Global Ajax Event Handlers

These methods register handlers to be called when certain events, such as initialization or completion, take place for any Ajax request on the page.
.ajaxComplete()Register a handler to be called when Ajax requests complete. This is an AjaxEvent.

.ajaxError()

Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.

.ajaxSend()

Attach a function to be executed before an Ajax request is sent. This is an Ajax Event.

.ajaxStart()

Register a handler to be called when the first Ajax request begins. This is an Ajax Event.

.ajaxStop()

Register a handler to be called when all Ajax requests have completed. This is an Ajax Event.

.ajaxSuccess()

Attach a function to be executed whenever an Ajax request completes successfully. This is an Ajax Event.

Example:

jQuery(document).ajaxSuccess(function() {
alert(‘success’);
});

Working with API with PHP & jQuery

/*jQuery Code*/
$.ajax({
type: ‘POST’,
url: re_url,
data:{“property”: “value”},
}).success(function(result){
$(“.data_div”).html(result);
});

/*PHP code*/
$url = “API URL”;
$doump_values = file_get_contents($url);
$json = json_decode($new_results);

echo “Value: “.$json->property;