JavaScript III
JavaScript III
• Event Handling
• Submit event
• JavaScript display properties
• Data validation
Events
Browser User
events Webpage events
loads or Clicking a
unloads button
Pressing any
Resizing a
key on the
window
keyboard
Event Handler attribute
• Basic syntax
<element event = ‘JavaScript’>
<element event = “JavaScript”>
Common HTML Events
Type Event Description
Form events
To submit the form
onsubmit
onchange
To change the value of a form element
onclick
• <element onclick="Script">
HTML • <button onclick="document.getElementById(
'demo').innerHTML =
Date()">Time?</button>
<html>
<head>
<h1> JavaScript onmouseover Event </h1>
</head>
<body>
<script language="Javascript" type="text/Javascript">
onmouseover.htm
function mouseoverevent()
{
alert("Demo of onmouseover");
}
</script>
<p onmouseover="mouseoverevent()"> Keep cursor over text</p>
</body>
</html>
Onfocus
<html>
<head> Javascript onfocus event</head> onfocus.htm
<body>
<h2> Input</h2>
<input type="text" id="a" onfocus="focusevent()"/>
<script>
function focusevent()
{
document.getElementById("a").style.background="pink";
}
</script>
</body>
</html>
onkeydown
<html>
<head> Javascript onkeydown Event</head>
<body>
<h2> Input</h2>
<input type="text" id="a" onkeydown="keydownevent()"/>
<script>
onkeydown.htm
function keydownevent()
{
document.getElementById("a");
alert(“Demo of onkeydown");
}
</script>
</body>
</html>
onload
<html>
<head>Javascript Events</head>
</br>
<body onload="window.alert('Page loaded
successfully');">
<script>
document.write("Demonstration of onload");
</script>
</body> onload.htm
</html>
Submit event
onsubmit
Occurs when a form is
Basics
submitted
• In HTML:
<element
onsubmit=“script_new”>
Syntax
• In JavaScript:
object.onsubmit =
function(){script_new};
onsubmit event (contd.)
• Example
<!DOCTYPE html>
<html>
<body>
<form onsubmit="formFunction()">
Enter name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
<script>
function formFunction() {
alert("Form submitted");
}
</script>
</body>
</html>
onsubmit event (contd.)
<!DOCTYPE html>
<html>
<body>
<p>onsubmit event</p>
<form id="new_Form">
Enter name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
<script>
document.getElementById("new_Form").onsubmit = function() {form_Function()};
function form_Function() {
alert("Form submitted");
}
</script>
</body>
</html>
JavaScript Display Properties
• innerHTML:
defines HTML content
Using document.getElementById method
Id attribute defines the HTML element
Use of innerHTML (contd.)
Example
<!DOCTYPE html>
<html>
<body>
<h2>Use of innerHTML</h2>
<p><b>Paragraph content</b></p>
<p id="js"></p>
<script>
document.getElementById("js").innerHTML = "Paragraph
content is changed into the First JavaScript";
</script>
</body>
</html>
Use of document.write()
<h2>Demonstration of document.write</h2>
<script>
document.write(50*20);
</script>
</body>
</html>
Use of window.alert()
• Display alert box in an alert box
• Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Data Validation
Need
To validate
To authenticate user
inappropriate values
To validate name,
To validate form on password, email, date,
client side mobile number, address
and other fields
Data Validation
Functions
Basic validation
To check mandatory fields are
filled in
<!DOCTYPE html>
<html>
<body>
<form>
<label for="pin">PIN:</label><br>
<input type="text" id="pin" name="pin" maxlength="5"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Form Attributes (contd.)
• Syntax: /pattern/modifiers;
• Google/i
• Example:
<!DOCTYPE html>
<html>
<body>
<h2>pattern attribute</h2>
<form>
<label for="code">Country code:</label>
<input type="text" id="code" name="code" pattern="[A-Za-
z]{3}"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Form Attributes (contd.)
• Example:
<!DOCTYPE html>
<html>
<body>
<h2>Autofocus attribute</h2>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" autofocus><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Password Validation
Must be alphanumeric
Basics
First letter of the password must be capital
if(firstpassword==secondpassword){
return true;
}
else{
alert("password must be same!");
return false;
}
}
</script>
RegExp Object:
Regular expression:
Pattern
Pattern of characters
with Properties and Methods
Syntax Example
/pattern/modifier(s); let pattern = /JavaScript/i;
Email validation
Example
<!DOCTYPE html>
<html>
<body>
<form >
E-mail: <input type="email" id=“abc" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-
z]{2,3}$">
<input type="submit">
</form>
<button onclick=“func_validate()">Try it</button>
<p id="demo"></p>
<script>
function func_validate() {
var x = document.getElementById(“abc").pattern;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>