Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
146 views6 pages

JavaScript Examples

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 6

JavaScript

Form Validation

JavaScript can be used to validate data in HTML forms before sending off the content to
a server.

Form data that typically are checked by a JavaScript could be:

 has the user left required fields empty?


 has the user entered a valid e-mail address?

 has the user entered a valid date?

 has the user entered text in a numeric field?

Required Fields

The function below checks if a field has been left empty. If the field is blank, an alert box
alerts a message, the function returns false, and the form will not be submitted:

function validateForm()
{
var x=document.forms["myForm"]["fname"].value
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}

E-mail Validation

The function below checks if the content has the general syntax of an email.

This means that the input data must contain an @ sign and at least one dot (.). Also, the
@ must not be the first character of the email address, and the last dot must be present
after the @ sign, and minimum 2 characters before the end:

function validateForm()
{
var x=document.forms["myForm"]["email"].value
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;

1
}
}

JavaScript Events
1. Onclick event
<html>
<head>
<script type="text/javascript">
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body>

<h1>My First Web Page</h1>


<p id="demo">This is a paragraph.</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html>
2. Mouse over event
<html>
<head>
<script type="text/javascript">
function writeText(txt)
{
document.getElementById("desc").innerHTML=txt;
}
</script>
</head>

<body>
<img src ="planets.gif" width ="145" height ="126" alt="Planets"
usemap="#planetmap" />

<map name="planetmap">
<area shape ="rect" coords ="0,0,82,126"
onmouseover="writeText('The Sun and the gas giant planets like Jupiter are by far
the largest objects in our Solar System.')"
href ="sun.htm" target ="_blank" alt="Sun" />

<area shape ="circle" coords ="90,58,3"

2
onmouseover="writeText('The planet Mercury is very difficult to study from the
Earth because it is always so close to the Sun.')"
href ="mercur.htm" target ="_blank" alt="Mercury" />

<area shape ="circle" coords ="124,58,8"


onmouseover="writeText('Until the 1960s, Venus was often considered a twin sister
to the Earth because Venus is the nearest planet to us, and because the two planets
seem to share many characteristics.')"
href ="venus.htm" target ="_blank" alt="Venus" />
</map>

<p id="desc">Mouse over the sun and the planets and see the different
descriptions.</p>

</body>
</html>

3. Alert Boxes
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Hello! I am an alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body>
</html>

4. Confirm Box
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
3
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show a confirm box" />

</body>
</html>

OBJECTS

1. Window (open a new window when clicking on a button)

<html>
<head>
<script type="text/javascript">
function open_win()
{
window.open("http://www.w3schools.com");
}
</script>
</head>

<body>
<form>
<input type=button value="Open Window" onclick="open_win()">
</form>
</body>

</html>

2. Open a new window and control its appearance


<html>
<head>
<script type="text/javascript">
function open_win()
{
window.open("http://www.w3schools.com","_blank","toolbar=yes, location=yes,
directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes,
width=400, height=400");
}
</script>
</head>

<body>
<form>
4
<input type="button" value="Open Window" onclick="open_win()">
</form>
</body>

</html>

3. Creating back button on a page


<html>
<head>
<script type="text/javascript">
function goBack()
{
window.history.back()
}
</script>
</head>
<body>

<input type="button" value="Back" onclick="goBack()" />

<p>Notice that clicking on the Back button here will not result in any action, because
there is no previous URL in the history list.</p>

</body>
</html>

4. Form Object Submit form


<html>
<head>
<script type="text/javascript">
function goBack()
{
window.history.back()
}
</script>
</head>
<body>

<input type="button" value="Back" onclick="goBack()" />

<p>Notice that clicking on the Back button here will not result in any action, because
there is no previous URL in the history list.</p>

</body>
</html>

5
5. Reset Form
<html>
<head>
<script type="text/javascript">
function formReset()
{
document.getElementById("frm1").reset();
}
</script>
</head>
<body>

<p>Enter some text in the fields below, then press the "Reset form" button to reset the
form.</p>

<form id="frm1">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br /><br />
<input type="button" onclick="formReset()" value="Reset form" />
</form>

</body>
</html>

Frames
Change the height and width of iframe
<html>
<head>
<script type="text/javascript">
function changeSize()
{
document.getElementById("myframe").height="300";
document.getElementById("myframe").width="300";
}
</script>
</head>
<body>
<iframe id="myframe" src="/default.asp" height="200" width="200">
<p>Your browser does not support iframes.</p>
</iframe>
<br /><br />
<input type="button" onclick="changeSize()" value="Change size" />
</body>
</html>

You might also like