JavaScript

Thursday, September 16, 2010

Category Tree

Hello Everybody,
There are many occasion when we required hierarchical structure.
In Which we need multilevel structure.E-commerce websites frequently
use this functionality to show various categories of their items.
what we need is to having all child node under its respective parent
such that one can easily differentiate each category-subcategory.
We can achieve this using single table with three fields. i.e
-------------------------------
Id-------Category--------Parent
1            Parent1                  0
2            Child1-Parent1       1
3            subchild-Child1       2
4            Child2-Parent1       1
5            Parent2                 0
6            Child1-Parent2      5
7            subchild1-child1    6
8            subchild2-child1    6
All set..But how to achieve structure as follows:
Parent1
|_Child1-Parent1
|_|_subchild-Child1
|_Child2-Parent1
Parent2
|_Child1-Parent2
|_|_subchild1-child1
|_|_subchild1-child1

Source: ClickHere to see the code.

Sunday, August 15, 2010

Disable Right Click

Disable Your Right-mouse Click


Hi, Friends

Here is the javascript to disable right click functionality of the system.
It may useful in application that require security or prevention to copy images etc..
To make use of this script copy this script to your body section.
Use script tag & language = javascrpt.
var message="Function is Disabled!";

function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")

Sunday, August 8, 2010

Validation using Javascript

Hello Friends,


What is Javascript?
If you are non-programmer then i would simply like to say Javascript is cripting language of the Web.
Moreover it is an easy to use programming language that can be embeddedin the headet part of your
web pages.(i.e HTML). JavaScript allows one to add vast functionalities to webpages.We can add
many features as per liking using JavaScript such as validating forms, image rotations, creation of
Cookie and more...

Here i am giving you basic Validation of form using JavaScript.
Example:
copy the following code in head tag of your page.


function formValidator(){

// Make quick references to our fields

var firstname = document.getElementById('firstname');

var addr = document.getElementById('addr');

var zip = document.getElementById('zip');

var state = document.getElementById('state');

var username = document.getElementById('username');

var email = document.getElementById('email');


// Check each input in the order that it appears in the form!

if(isAlphabet(firstname, "Please enter only letters for your name"))

{

   if(isAlphanumeric(addr, "Numbers and Letters Only for Address"))

   {

     if(isNumeric(zip, "Please enter a valid zip code"))

        {

            if(lengthRestriction(zip, 5, 6,"Please enter zip between 5-6 digits"))

              {

                  if(madeSelection(state, "Please Choose a State"))

                   {

                       if(lengthRestriction(username, 6, 8,"Please enter User name between 6-8 charactar"))

                         {

                           if(emailValidator(email,"Please enter a valid email address"))

                            {

                             return true;

                             }

                         }

                    }

              }

          }

     }

  }


return false;


}

function notEmpty(elem, helperMsg){

if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;

}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{

alert(helperMsg);

elem.focus();

return false;

}

}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;

}

}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}

}


function lengthRestriction(elem, min, max,helperMsg){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){ return true; }
else{ alert(helperMsg);//"Please enter between " +min+ " and " +max+ " characters" elem.focus(); return false; } }


function madeSelection(elem, helperMsg){
if(elem.value == “Please Choose”){
alert(helperMsg);
elem.focus();
return false;
}else
{
return true;

}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else
{
alert(helperMsg);
elem.focus();
return false;
}
}

Now Create form as listed below in your Web-page.use the same id for each element in JavaScript that you describe in HTML part.i.e for Firstname id="firstname".

First Name:  
Address:  
Zip (5-6 char):  
State:  
Username(6-8 char):  
Email:


Submit Site to Google