JavaScript Examples
JavaScript Examples
JavaScript Examples
Form Validation
JavaScript can be used to validate data in HTML forms before sending off the content to
a server.
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>
</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" />
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" />
<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>
</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>
</body>
</html>
OBJECTS
<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>
<body>
<form>
4
<input type="button" value="Open Window" onclick="open_win()">
</form>
</body>
</html>
<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>
<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>