Here you can find a number of examples on Java Script.Please find the various kinds of examples listed on the right side of this site. If you don't find anything on Java Script then please write to us or leave a commnet. We are pleased to help and find the solution of your problem.
Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

JavaScript: Validation

<html>
<head>
<title>Enquiry Form</title>
<script language="javascript">
function check()
{
var email=f1.email.value;
if(f1.fname.value=="")
alert("Please enter first name");
else
if(f1.lname.value=="")
alert("Please enter last name");
else
if(f1.cno.value=="")
alert("Please enter contact number");
else
if(isNaN(f1.cno.value))
alert("Please enter correct contact number");
else
if(f1.add.value=="")
alert("Please enter address");
else
if(f1.email.value=="")
alert("Please enter Email address");
else
if(email.indexOf("@")==-1)
alert("Missing '@' in Email address");
else
if(email.indexOf(".")==-1)
alert("Missing '.' in Email address");
else
if(f1.course.value=="select")
alert("Please select the course name");
else
if(f1.d.value=="Day")
alert("Please enter day");
else
if(f1.M.value=="month")
alert("Please enter Month");
else
if(f1.y.value=="year")
alert("Please enter Year");
}

</script>
</head>
<body>
<h1>Enquiry Form</h1><p>
<form name="f1" action="mailto:yourmailid@sitename.com" method="post">
<table width="60%">
<tr>
<td width="50%">First Name: </td><td width="50%"><input type="text" name=fname></td></tr>
<tr>
<td>Last Name: </td><td><input type="text" name=lname></td></tr>
<tr><td>Gender: </td><td><input type="radio" name=g checked>Male <input type="radio" name=g>Female</td></tr>
<tr><td>PhoneNumber:</td><td><input type="text" name=cno></td></tr>
<tr><td>Address: </td><td><textarea name="add"></textarea></td></tr>
<tr><td>Email-id</td><td><input type="text" name=email style="background-color:yellow"></td></tr>
<tr><td>Date of Birth: </td><td>
<select name="d">
<OPTION value="Day" selected>Day</OPTION>
<OPTION value=1>1</OPTION> <OPTION value=2>2</OPTION>
<OPTION value=3>3</OPTION> <OPTION value=4>4</OPTION>
<OPTION value=5>5</OPTION> <OPTION value=6>6</OPTION>
<OPTION value=7>7</OPTION> <OPTION value=8>8</OPTION>
<OPTION value=9>9</OPTION> <OPTION
value=10>10</OPTION> <OPTION value=11>11</OPTION>
<OPTION value=12>12</OPTION> <OPTION
value=13>13</OPTION> <OPTION value=14>14</OPTION>
<OPTION value=15>15</OPTION> <OPTION
value=16>16</OPTION> <OPTION value=17>17</OPTION>
<OPTION value=18>18</OPTION> <OPTION
value=19>19</OPTION> <OPTION value=20>20</OPTION>
<OPTION value=21>21</OPTION> <OPTION
value=22>22</OPTION> <OPTION value=2>23</OPTION>
<OPTION value=24>24</OPTION> <OPTION
value=25>25</OPTION> <OPTION value=26>26</OPTION>
<OPTION value=27>27</OPTION> <OPTION
value=28>28</OPTION> <OPTION value=29>29</OPTION>
<OPTION value=30>30</OPTION> <OPTION
value=31>31</OPTION></SELECT>
<select name="M">
<OPTION value="month" selected>Month</OPTION> <OPTION
value=Jan>Jan</OPTION> <OPTION value=Feb>Feb</OPTION>
<OPTION value=March>March</OPTION> <OPTION
value=April>April</OPTION> <OPTION
value=May>May</OPTION> <OPTION
value=June>June</OPTION> <OPTION
value=July>July</OPTION> <OPTION
value=Aug>Aug</OPTION> <OPTION
value=Sept>Sept</OPTION> <OPTION
value=Oct>Oct</OPTION> <OPTION value=Nov>Nov</OPTION>
<OPTION value=Dec>Dec</OPTION></SELECT>
<select name="y">
<OPTION value="year" selected>Year</OPTION>
<OPTION value=2008>2008</OPTION> <OPTION
value=2007>2007</OPTION> <OPTION
value=2006>2006</OPTION> <OPTION
value=2005>2005</OPTION> <OPTION
value=2004>2004</OPTION> <OPTION
value=2003>2003</OPTION> <OPTION
value=2002>2002</OPTION> <OPTION
value=2001>2001</OPTION> <OPTION
value=2000>2000</OPTION> <OPTION
value=1999>1999</OPTION> <OPTION
value=1998>1998</OPTION> <OPTION
value=1997>1997</OPTION> <OPTION
value=1996>1996</OPTION> <OPTION
value=1995>1995</OPTION> <OPTION
value=1994>1994</OPTION> <OPTION
value=1993>1993</OPTION> <OPTION
value=1992>1992</OPTION> <OPTION
value=1991>1991</OPTION> <OPTION
value=1990>1990</OPTION> <OPTION
value=1989>1989</OPTION> <OPTION
value=1988>1988</OPTION> <OPTION
value=1987>1987</OPTION> <OPTION
value=1986>1986</OPTION> <OPTION
value=1985>1985</OPTION> <OPTION
value=1984>1984</OPTION> <OPTION
value=1983>1983</OPTION> <OPTION
value=1982>1982</OPTION> <OPTION
value=1981>1981</OPTION> <OPTION
value=1980>1980</OPTION> <OPTION
value=1979>1979</OPTION> <OPTION
value=1978>1978</OPTION> <OPTION
value=1977>1977</OPTION> <OPTION
value=1976>1976</OPTION> <OPTION
value=1975>1975</OPTION> <OPTION
value=1974>1974</OPTION> <OPTION
value=1973>1973</OPTION> <OPTION
value=1972>1972</OPTION> <OPTION
value=1971>1971</OPTION> <OPTION
value=1970>1970</OPTION> <OPTION
value=1969>1969</OPTION> <OPTION
value=1968>1968</OPTION> <OPTION
value=1967>1967</OPTION> <OPTION
value=1966>1966</OPTION> <OPTION
value=1965>1965</OPTION> <OPTION
value=1964>1964</OPTION> <OPTION
value=1963>1963</OPTION> <OPTION
value=1962>1962</OPTION> <OPTION
value=1961>1961</OPTION> <OPTION
value=1960>1960</OPTION> <OPTION
value=1959>1959</OPTION> <OPTION
value=1958>1958</OPTION> <OPTION
value=1957>1957</OPTION> <OPTION
value=1956>1956</OPTION> <OPTION
value=1955>1955</OPTION> <OPTION
value=1954>1954</OPTION> <OPTION
value=1953>1953</OPTION> <OPTION
value=1952>1952</OPTION> <OPTION
value=1951>1951</OPTION> <OPTION
value=1950>1950</OPTION> <OPTION
value=1949>1949</OPTION> <OPTION
value=1948>1948</OPTION> <OPTION
value=1947>1947</OPTION> <OPTION
value=1946>1946</OPTION> <OPTION
value=1945>1945</OPTION> <OPTION
value=1944>1944</OPTION> <OPTION
value=1943>1943</OPTION> <OPTION
value=1942>1942</OPTION> <OPTION
value=1941>1941</OPTION> <OPTION
value=1940>1940</OPTION> <OPTION
value=1939>1939</OPTION> <OPTION
value=1938>1938</OPTION> <OPTION
value=1937>1937</OPTION> <OPTION
value=1936>1936</OPTION> <OPTION
value=1935>1935</OPTION> <OPTION
value=1934>1934</OPTION> <OPTION
value=1933>1933</OPTION> <OPTION
value=1932>1932</OPTION> <OPTION
value=1931>1931</OPTION> <OPTION
value=1930>1930</OPTION> <OPTION
value=1929>1929</OPTION> <OPTION
value=1928>1928</OPTION> <OPTION
value=1927>1927</OPTION> <OPTION
value=1926>1926</OPTION> <OPTION
value=1925>1925</OPTION> <OPTION
value=1924>1924</OPTION> <OPTION
value=1923>1923</OPTION> <OPTION
value=1922>1922</OPTION> <OPTION
value=1921>1921</OPTION> <OPTION
value=1920>1920</OPTION> <OPTION
value=1919>1919</OPTION> <OPTION
value=1918>1918</OPTION> <OPTION
value=1917>1917</OPTION> <OPTION
value=1916>1916</OPTION> <OPTION
value=1915>1915</OPTION> <OPTION
value=1914>1914</OPTION> <OPTION
value=1913>1913</OPTION> <OPTION
value=1912>1912</OPTION> <OPTION
value=1911>1911</OPTION> <OPTION
value=1910>1910</OPTION> <OPTION
value=1909>1909</OPTION> <OPTION
value=1908>1908</OPTION> <OPTION
value=1907>1907</OPTION> <OPTION
value=1906>1906</OPTION> <OPTION
value=1905>1905</OPTION> <OPTION
value=1904>1904</OPTION> <OPTION
value=1903>1903</OPTION> <OPTION
value=1902>1902</OPTION> <OPTION
value=1901>1901</OPTION></SELECT>
</td></tr>
<tr><td><center>
<input type="submit" value="Submit" onclick="check()"> </center></td><td>    
<input type="reset" value="reset"></td></tr>
</table>
</form>
</body>
</html>

JavaScript: splice Function



Splice Function:
Changes the content of an array,adding new elements while removing old elements.

Syntax:
splice(index,howmany,"element1","element2",......,"elementN");

Parameters:
index:Index at which to start changing the array.
howMany:An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element.
element1, ....,elementNThe element to add to the array. If you don't specify any elements, splice simply removes elements from the array.



Example in HEAD section:

<html>
<head>
<title>Splice Function</title>
<script language="javascript">
arr=new Array("one","two","three","Four");
document.write("<b>My Array before:</b> " + arr);
arr.splice(4,0,"five","six");
document.write("<br><b>my Array after:</b> " + arr);

document.write("<p><b>My Array before:</b> " + arr);
arr.splice(0,2,"1","2");
document.write("<br><b>my Array after:</b> " + arr);
</script>
</head>
</html>


Example in BODY section:

<html>
<head>
<title>Splice Function</title>
</head>
<body>
<script language="javascript">
arr=new Array("one","two","three","Four");
document.write("<b>My Array before:</b> " + arr);
arr.splice(4,0,"five","six");
document.write("<br><b>my Array after:</b> " + arr);

document.write("<p><b>My Array before:</b> " + arr);
arr.splice(0,2,"1","2");
document.write("<br><b>my Array after:</b> " + arr);
</script>
</body>
</html>



OUTPUT:
My Array before: one,two,three,Four
my Array after: one,two,three,Four,five,six

My Array before: one,two,three,Four,five,six
my Array after: 1,2,three,Four,five,six

JavaScript: Shift Function



shift Function:
Removes the first element from an array and returns that element. This method changes the length of the array.

Syntax:
arrayname.shift()



Example in HEAD section:

<html>
<head>
<title>Shift Function</title>
<script language="javascript">
myArray = ["angel", "clown", "mandarin", "surgeon"];
document.writeln("myArray before: " + myArray + "<p>");
shifted = myArray.shift();
document.writeln("myArray after: " + myArray + "<p>");
document.writeln("Removed this element: " + shifted);
</script>
</head>
</html>


Example in BODY section:

<html>
<head>
<title>Shift Function</title>
</head>
<body>
<script language="javascript">
myArray = ["angel", "clown", "mandarin", "surgeon"];
document.writeln("myArray before: " + myArray + "<p>");
shifted = myArray.shift();
document.writeln("myArray after: " + myArray + "<p>");
document.writeln("Removed this element: " + shifted);
</script>
</body>
</html>




OUTPUT

myArray before: angel,clown,mandarin,surgeon

myArray after: clown,mandarin,surgeon

Removed this element: angel

JavaScript: Power Function



pow function:
Returns base to the exponent power, that is, baseexponent .
Calculate x raised to y (xy)

Syntax:
pow(x,y)
x is the base number and y is the exponent to which to raise base.

Description:
Because pow is a static method of Math, you always use it as Math.pow(), rather than as a method of a Math object you created.



Example:

<html>
<head>
<title>Power Function</title>

<script language="javascript">
document.write("calculate 5 <sup> 2 </sup> = " + Math.pow(5,2) + "<br>");
document.write("calcuclate 3 <sup> 5 </sup> = " + Math.pow(3,5) + "<br>");
</script>

</head>
</html>



OUTPUT:

calculate 52 = 25
calcuclate 35 = 243

JavaScript: Min Function



Math.min() Function:
Find the minimum number in the array list.

Syntax:
min(number1,number2,.......numbern)

Description:
Because min is a static method of Math, you always use it as Math.min(), rather than as a method of a Math object you created.



Example

<html>
<head>
<title>MIN Function</title>

<script language="javascript">
document.write("The minimum number is " + Math.min(5,8) + " from(5,8)
");
document.write("The minimum number is " + Math.min(-2,-9) + " from(-2,-9)
");
document.write("The minimum number is " + Math.min(7.25,7.30) + " from(7.25,7.30)");
</script>

</head>
</html>



OUTPUT:

The minimum number is 5 from(5,8)
The minimum number is -9 from(-2,-9)
The minimum number is 7.25 from(7.25,7.30)

JavaScript: MAX Function



Math.max() Function:
Find the maximum number in the arraylist.
Syntax:
max(number,numner.........,number)

Description:
Because max is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created.



Example:

<html>
<head>
<title>Math.max() Function</title>

<script language="javascript">
document.write("The maximum number is " + Math.max(5,8,10) + " from(5,8,10)
");
document.write("The maximum number is " + Math.max(-4,-7) + " from(-4,-7)
");
</script>

</head>
</html>



OUT PUT

The maximum number is 10 from(5,8,10)
The maximum number is -4 from(-4,-7)

JavaScript: isNaN Function



isNaN Function :-
Evaluates an argument to determine if it is not a number.

Syntax :-
isNaN(value)
value = The value you want to evaluate.

Description :-
isNaN is a top-level function and is not associated with any object.

On platforms that support NaN, the parseFloat and parseInt functions return NaN when they evaluate a value that is not a number. isNaN returns true if passed NaN, and false otherwise.



Example:

<html>
<head>
<title>isNaN Fnction</title>

<script language="javascript">
function check()
{
var phone=f1.phone.value;
if(isNaN(phone))
alert("Characters are not allowed in the Phone Number");
else
alert("Your Phone Number is Accepted");
}
</script>

</head>
<body>

<form name="f1">
Phone: <input type="text" name="phone" size="20">
<input type="button" value="check" onclick="check()">
</form>

</body>
</html>

JavaScript: Squareroot Function



sqrt Function :-
Returns the square root of a number.

Suntax :-
sqrt(number)

Description :-
If the value of number is negative,sqrt returns NaN. Because sqrt is a static method of Math,you always use it as Math.sqrt(), rather than as a method of a Math object you created.



Example:

<html>
<head>
<title>Calculate Squareroot</title>

<script language="javascript">
function calcsqrt()
{
var value=f1.sqrt.value;
alert("The Squareroot of " + value + " is " + Math.sqrt(value));
}
</script>

</head>
<body>

<form name="f1">
<input type="text" name="sqrt" value="Enter Number" size="20">
<input type="button" value="Calculate" onclick="calcsqrt()">
</form>

</body>
</html>

JavaScript: PI Function



PI:
The ratio of the circumference of a circle to its diameter, approximately 3.14159.

Description:
Because PI is a static property of Math, you always use it as Math.PI, rather than as a property of a Math object you created.



Example

<html>
<head>
<title>Calculate PI Value</title>

<script language="javascript">
document.write(Math.PI);
</script>

</head>
</html>


OUTPUT

3.141592653589793

Javascript: FLOOR Function



floor function Returns the largest integer less than or equal to a number.

Syntax:
floor(a number)

Description:
Because floor is a static method of Math, you always use it as Math.floor(), rather than as a method of a Math object you created.


Example in HEAD Section

<html>
<head>
<title>Math.floor function</title>
<script languag="javascript">
document.write("Pass value is -45.65   and return value is = " + Math.floor(45.65) + "<br>");
document.write("Pass value is -45.65   and return value is = " + Math.floor(-45.65));
</script>
</head>
</html>


Example in BODY Section

<html>
<head>
<title>Math.floor function</title>
</head>
<body>
<script languag="javascript">
document.write("Pass value is -45.65   and return value is = " + Math.floor(45.65) + "<br>");
document.write("Pass value is -45.65   and return value is = " + Math.floor(-45.65));
</script>
</body>
</html>




OUTPUT

Pass value is -45.65 and return value is = 45
Pass value is -45.65 and return value is = -46

Javascript: CEIL Function



ceil Function returns the smallest integer greater than or equal to a number.

Syntax:
ceil(a numebr)

Description:
Because ceil is a static method of math, you always use it as Math.ceil(), rather than as a method of a Math object you created.


Example in HEAD Section
<html>
<head>
<title>Math.ceil function</title>
<script language="javascript">
document.write("Pass value is -45.65   and return value is = " + Math.ceil(45.65) + "<br>");
document.write("Pass value is -45.65   and return value is = " + Math.ceil(-45.65));
</script>
</head>
</html>


Example in BODY Section
<html>
<head>
<title>Math.ceil function</title>
</head>
<body>
<script language="javascript">
document.write("Pass value is -45.65   and return value is = " + Math.ceil(45.65) + "<br>");
document.write("Pass value is -45.65   and return value is = " + Math.ceil(-45.65));
</script>
</body>
</html>




OUTPUT

Pass value is -45.65 and return value is = 46
Pass value is -45.65 and return value is = -45

Javascript: BIG Function



big Funtion
                 
Causes a string to be displayed in a big font as if it were in a BIG tag.

Syntax:
big()

Description:
                   
Use the big method with the write or write methods to format and display a string in a document. In server-side JavaScript, use the write function to display the string.



Example:

<html>
<head>
<title>Big function</title>

<script languge="javscript">
function makebig()
{
var name=f1.t1.value.big();
document.write(name);
}
</script>

</head>
<body>

<form name="f1">
<input type="text" name="t1" value="click" size="20">
<input type="button" onclick="makebig()" value="click">
</form>

</body>
</html>
Google