0% found this document useful (0 votes)
20 views

Javascript Programs

This document contains code to: 1) Open a popup window using JavaScript when a link is clicked and write text to the popup. 2) Check if the popup window is already closed and close it or display a message accordingly when another link is clicked. 3) Validate an email address field in a form by checking for valid syntax and displaying a message if invalid. 4) Get number values from two text fields, add them together using JavaScript, and display the result in a third field when a button is clicked.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Javascript Programs

This document contains code to: 1) Open a popup window using JavaScript when a link is clicked and write text to the popup. 2) Check if the popup window is already closed and close it or display a message accordingly when another link is clicked. 3) Validate an email address field in a form by checking for valid syntax and displaying a message if invalid. 4) Get number values from two text fields, add them together using JavaScript, and display the result in a third field when a button is clicked.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

<html>

<head>
<title>Input tutorial</title>
<script language="javascript">

function addNumbers()
{
var val1 =
parseInt(document.getElementById("value1").value);

var val2 =
parseInt(document.getElementById("value2").value);

var ansD = document.getElementById("answer");

ansD.value = val1 + val2;


}
</script>
</head>
<body>

value1 = <input type="text" id="value1" name="value1" value="1"/>


value2 = <input type="text" id="value2" name="value2" value="2"/>

<input type="button" name="Sumbit" value="Click here"


onclick="javascript:addNumbers()"/>

Answer = <input type="text" id="answer" name="answer" value=""/>


</body>
</html>

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;
 }
}

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();"


method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>

<html>
<head>
   <title>JavaScript Window Close Example </title>
</head>
<script type="text/javascript">
   function popuponclick()

   {
      my_window = window.open("",
       "mywindow","status=1,width=350,height=150");
      my_window.document.write('<h1>The Popup Window</h1>');
   }
   function closepopup()
   {
      if(false == my_window.closed)
      {
         my_window.close ();
      }
      else
      {
         alert('Window already closed!');
      }
   }
</script>
<body>

   <p>
      <a href="javascript: popuponclick()">Open Popup Window</a>
   </p>
   <p>
      <a href="javascript: closepopup()">Close the Popup Window</a>
   </p>
</body>
</html>

You might also like