Get url query string value in jquery

Suppose url is : http://www.example.com/?id=2&name=test

using the below function you can get the querystring value using jquery

function getquerystring(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split(‘&’);
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split(‘=’);
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}

alert(getquerystring(‘id’));

 

Show image preview without uploading to server using jQuery

screenshot

 

$(“#input_1_28”).on(‘change’, function() {
var countFiles = $(this)[0].files.length;
var imgPath = $(this)[0].value;
var extn = imgPath.substring(imgPath.lastIndexOf(‘.’) + 1).toLowerCase();
var image_holder = $(“#sdoc”);
image_holder.empty();
if (typeof(FileReader) != “undefined”) {
for (var i = 0; i < countFiles; i++)
{
var reader = new FileReader();
reader.onload = function(e) {
$(“<img />”, {
“src”: e.target.result,
“width”: 200,
“class”: “thumb-image”
}).appendTo(image_holder);
}
image_holder.show();
reader.readAsDataURL($(this)[0].files[i]);
}
} else {
alert(“This browser does not support FileReader.”);
}
});

Convert Hexcode to RGB values

This answer has been taken from Stackoverflow which helped me to convert hex to rgb format.

the below function you can use to convert but pass hex values without ‘#’ and get RGB values

Hexcode = ‘#00000’;

call function like

hexToRgb(00000);

function hexToRgb(hex) {
var bigint = parseInt(hex, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;
return r + “,” + g + “,” + b;
}

Result : ‘0,0,0’

Multiple Checkbox validation using Jquery

var chlen = jQuery(‘[name=”page_access[]”]:checked’).length;
if(chlen == 0)
{
document.getElementById(‘pagerr’).innerHTML='( Check any checkbox)’;
if(ids==”) { ids=’pagerr’; }
err++;
}

<input type=”checkbox” name=”page_access[]” value=”c1″>Check 1<br />
<input type=”checkbox” name=”page_access[]” value=”c2″>Check 2<br />

Allow Only Numbers to Enter From Input field

HTML Code

<input type=”text” class=”form-control” name=”phone” id=”phone” value=”” onkeypress=”return onlyNos(event,this);” maxlength=”13″ />

JavaScript Code

function onlyNos(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
catch (err) {
alert(err.Description);
}
}