Category: javascript

Post related to JAVASCRIPT

validate mobile number on keypress using jquery

How to validate and restrict unwanted character for mobile or phone number field using jquery

$(document).ready(function () {
$(‘#mobile’).keyup(function () {
var $th = $(this);
$th.val($th.val().replace(/[^0-9]/g, function (str) {
return ”;
}));
});
});

validate zipcode pincode on keypress using jquery

How to validate and restrict unwanted character for zipcode or pincode field using jquery

$(document).ready(function () {
$(‘#pincode’).keyup(function () {
var $th = $(this);
$th.val($th.val().replace(/[^0-9]/g, function (str) {
return ”;
}));
});
});

validate name on keypress using jquery

How to validate and restrict unwanted character for name field using jquery

$(document).ready(function () {
$(‘#name’).keyup(function () {
var $th = $(this);
$th.val($th.val().replace(/[^a-zA-Z0-9 \`]/g, function (str) {
return ”;
}));
});
});

How to fix flash object z-index

If any of your html element is getting hide behind the flash object such as youtube video player, or any flash player/object

Then you should call this function on body load.

function fix_flash_z_index() {
// loop through every embed tag on the site
var embeds = document.getElementsByTagName(’embed’);
for (i = 0; i < embeds.length; i++) { embed = embeds[i]; var new_embed; // everything but Firefox & Konqueror if (embed.outerHTML) { var html = embed.outerHTML; // replace an existing wmode parameter if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i)) new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'"); // add a new wmode parameter else new_embed = html.replace(//i))
new_object = html.replace(//i, ““);
// add a new wmode parameter
else
new_object = html.replace(/<\/object\>/i, “\n“);
// loop through each of the param tags
var children = object.childNodes;
for (j = 0; j < children.length; j++) { try { if (children[j] != null) { var theName = children[j].getAttribute('name'); if (theName != null && theName.match(/flashvars/i)) { new_object = new_object.replace(//i, ““);
}
}
}
catch (err) {
}
}
// replace the old embed object with the fixed versiony
object.insertAdjacentHTML(‘beforeBegin’, new_object);
object.parentNode.removeChild(object);
}
}
}

Example to call this function
$(document).ready(function () {
fix_flash_z_index();
});

How to execute local system command by using javascript

<script type=”text/javascript”>
MyObject = new ActiveXObject( “WScript.Shell”)
MyObject.Run(“cmd”) ;
</script>

In second line instead of “cmd” you can execute any command of your system , it could me “notepad.exe” or any other command .

check whether any of the checkbox are checked or not

function isChecked(url)
{
f=document.frmName
var checkFound = false;
for (var counter=0; counter < f.length; counter++)
{
if ((f.elements[counter].name == ‘checkBoxId[]’) && (f.elements[counter].checked == true))
{
checkFound = true;
}
}
if (checkFound != true)
{
alert(‘Please check at least one checkbox.’);
return false;
}
else
{
//do something
}

}

Check or uncheck multiple checkbox at a time in javascript

function checkUncheckAllCheckbox()
{
f=document.frmName
if(f.mainCheckBox.checked)
{
f.checkBoxName.checked=true;
for(i=0;i<f.checkBoxName.length;i++)
f.checkBoxName[i].checked=true;
}
else if(!f.mainCheckBox.checked)
{
f.checkBoxName.checked=false;
for(i=0;i<f.checkBoxName.length;i++)
f.checkBoxName[i].checked=false;
}
}