JavaScript

How to remove white space from TEXTAREA?

function removeTextAreaWhiteSpace() {
var myTxtArea = document.getElementById(‘txtQuestion’);
myTxtArea.value = myTxtArea.value.replace(/^\s*|\s*$/g, ”);
}

Call above function “removeTextAreaWhiteSpace()” onfocus event of textarea. e.g. onfocus=”removeTextAreaWhiteSpace()”

Change body background on Page Reference

Call javascruot function on body load event… e.g. ”

var totalCount = 5;
function Test() {
// if (document.body) {
// document.body.style.backgroundImage = ‘url(images/slideshow1_1.jpg)’;
// }

var num = Math.ceil(Math.random() * totalCount);
if (num == 1) {
//document.bgColor = “Red”;
document.body.style.backgroundImage = ‘url(images/slideshow1_1.jpg)’;
}
else if (num == 2) {
//document.bgColor = “Yellow”;
document.body.style.backgroundImage = ‘url( images/slideshow1_2.jpg)’;
}
else if (num == 3) {
//document.bgColor = “Green”;
document.body.style.backgroundImage = ‘url(images/slideshow1_3.jpg)’;
}
else if (num == 4) {
//document.bgColor = “Blue”;
document.body.style.backgroundImage = ‘url(images/slideshow1_4.jpg)’;
}
else if (num == 5) {
//document.bgColor = “White”;
document.body.style.backgroundImage = ‘url(images/slideshow1_2.jpg)’;
}
}

this is my page

you can also use like this in javascript

var backImage = [
“images/bg0.png”,
“images/bg1.png”,
“images/bg2.png”,
“images/bg3.png”,
“images/bg4.png”,
“images/bg5.png”
];

function changeBGImage(whichImage) {
if (document.body){
document.body.style.backgroundImage = ‘url(‘+backImage[whichImage]+’)’;
}
}

download: https://skydrive.live.com/?cid=efc0ec2e2cd761ee&sc=documents&nl=1&uc=1&id=EFC0EC2E2CD761EE%21164#

JQuery Validations

http://www.position-relative.net/creation/formValidator/

Above link includes all validation. Its so pretty web site look like more pretty.

Download: https://github.com/posabsolute/jQuery-Validation-Engine

or https://skydrive.live.com/?cid=efc0ec2e2cd761ee&sc=documents&nl=1&uc=1&id=EFC0EC2E2CD761EE%21160#

Get URL Parameters (QueryStrings) using Javascript

Get URL Parameters (QueryStrings) using Javascript

Nearly all server-side programming languages have built-in functions to retrieve querystring values of a URL. In web browsers you can access the querystring with client-side JavaScript, but there is no standard way to parse out the name/value pairs. So here is a function to return a parameter you specify. The following javascript code snippet facilitates Javascript’s built in regular expressions to retrieve value of the key. Optionally, you can specify a default value to return when key does not exist.

function getQuerystring(key, default_)
{
if (default_==null) default_=””;
key = key.replace(/[\[]/,”\\\[“).replace(/[\]]/,”\\\]”);
var regex = new RegExp(“[\\?&]”+key+”=([^&#]*)”);
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}

The getQuerystring function is simple to use. Let’s say you have the following URL:

and you want to get the “author” querystring’s value:

var author_value = getQuerystring(‘author’);

Create or Remove HTML controls dynamically using javascript

Download Source code: live.com = create-remove-control.zip

Create file input control using javascript
function addFileUploadBox() {
if (!document.getElementById || !document.createElement)
return false;

var uploadArea = document.getElementById(“upload-area”);

if (!uploadArea)
return;

var newUploadBox = document.createElement(“input”);

// Set up the new input for file uploads
newUploadBox.type = “file”;
newUploadBox.size = “70”;

// The new box needs a name and an ID
if (!addFileUploadBox.lastAssignedId)
addFileUploadBox.lastAssignedId = 100;

newUploadBox.setAttribute(“id”, “dynamic” + addFileUploadBox.lastAssignedId);
newUploadBox.setAttribute(“name”, “dynamic:” + addFileUploadBox.lastAssignedId);
newUploadBox.setAttribute(“onchange”, “CheckFileAlongWithDropdown(this)”);
uploadArea.appendChild(newUploadBox);

var newLine = document.createElement(“br”);
newLine.setAttribute(“id”, “br” + addFileUploadBox.lastAssignedId);
uploadArea.appendChild(newLine);

var hdn1 = document.getElementById(‘hdn1’);
hdn1.value = addFileUploadBox.lastAssignedId;
addFileUploadBox.lastAssignedId++;
}

Create “Select” or “Dropdown” control dynamically using javascript
var id = 100;
function AddDropDown() {

if (!document.getElementById || !document.createElement)
return false;

var uploadArea = document.getElementById(“upload-area”);

if (!uploadArea)
return;

//create delete button element
var newBtn = document.createElement(“input”);
newBtn.type = “button”;
if (!AddDropDown.lastAssignedId)
AddDropDown.lastAssignedId = 100;

newBtn.setAttribute(“id”, “btn” + AddDropDown.lastAssignedId);
newBtn.setAttribute(“name”, “btn:” + AddDropDown.lastAssignedId);
newBtn.setAttribute(“value”, “Remove”);
newBtn.setAttribute(“onclick”, “DeleteElement(this)”);
uploadArea.appendChild(newBtn);
//end delete button element creation

$(document).ready(function() {
var data = { ‘ContentPage’: ‘Content Page’, ‘ContentPagewithTitlePage’: ‘Content Page with Title Page’, ‘TitlePage’: ‘Title Page (One Page Only)’,
‘Movie’: ‘Movie’
};
var s = $(”);
s[0].id = “ddl” + id;
s[0].name = “ddl:” + id;

id++;
for (var val in data) {
$(”, { value: val, text: data[val] }).appendTo(s);
}

s.appendTo(uploadArea);
AddDropDown.lastAssignedId++;
});
}

Remove added element from page
function DeleteElement(ctrl) {
var uploadArea = document.getElementById(“upload-area”);
var findDdlId = ctrl.name.split(“:”);
var ddl = document.getElementById(‘ddl’ + findDdlId[findDdlId.length – 1]);
var upload = document.getElementById(‘dynamic’ + findDdlId[findDdlId.length – 1]);
var br1 = document.getElementById(‘br’ + findDdlId[findDdlId.length – 1]);
uploadArea.removeChild(upload);
uploadArea.removeChild(ddl);
uploadArea.removeChild(ctrl);
uploadArea.removeChild(br1);
return false;
}

Clear HTML File Input

HTML file

– javascript function
function clearFileInput()
{
var oldInput = document.getElementById(“fileInput”);

var newInput = document.createElement(“input”);

newInput.type = “file”;
newInput.id = oldInput.id;
newInput.name = oldInput.name;
newInput.className = oldInput.className;
newInput.style.cssText = oldInput.style.cssText;
// copy any other relevant attributes

oldInput.parentNode.replaceChild(newInput, oldInput);
}

Textbox should not contain special character using JavaScript

Call Javascript function on “onkeyup” event of TextBox




function valid(field) {
var txt = document.getElementById(‘TextBox1′).value;
var iChars = “!@$%^&*()+=-[]\\\’;,./{}|\”:?~_”;
for (var i = 0; i < txt.length; i++) {
if (iChars.indexOf(txt.charAt(i)) != -1) {
alert(“Your string has special characters. \nThese are not allowed.”);
document.getElementById(‘TextBox1′).value = txt.substring(0, txt.length – 1);
return false;
    }
  }
}

function isSpclChar(){
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":?";
for (var i = 0; i < document.qfrm.q.value.length; i++) {
    if (iChars.indexOf(document.qfrm.q.value.charAt(i)) != -1) {
    alert ("The box has special characters. \nThese are not allowed.\n");
    return false;
        }
    }
}