JavaScript Day7
JavaScript Day7
Day 7
JavaScript Forms
<!DOCTYPE html> </head>
<html><head><script> <body>
function validateForm() { <form name="myForm"
action="/action_page.php"
var x =
document.forms["myForm"] onsubmit="return validateForm()"
["fname"].value; method="post">
<!DOCTYPE html>
<html><body>
<form action="/action_page.php" method="post">
<input type="text" name="fname" required>
<input type="submit" value="Submit">
</form>
<p>If you click submit, without filling out the text field,
your browser will display an error message.</p>
</body></html>
Data Validation
Data validation is the process of ensuring that user input is clean,
correct, and useful.
Typical validation tasks are:
has the user filled in all required fields?
has the user entered a valid date?
has the user entered text in a numeric field?
Server side validation is performed by a web server, after input
has been sent to the server.
Client side validation is performed by a web browser, before input
is sent to a web server.
API
Property Description
customError Set to true, if a custom validity message is set.
patternMismatch Set to true, if an element's value does not match its pattern
attribute.
rangeOverflow Set to true, if an element's value is greater than its max
attribute.
rangeUnderflow Set to true, if an element's value is less than its min attribute.
stepMismatch Set to true, if an element's value is invalid per its step
attribute.
tooLong Set to true, if an element's value exceeds its maxLength
attribute.
typeMismatch Set to true, if an element's value is invalid per its type
attribute.
valueMissing Set to true, if an element (with a required attribute) has no
value.
valid Set to true, if an element's value is valid.
Example
<!DOCTYPE html> if
(document.getElementById("id1").validity.range
<html>
Overflow) {
<body>
txt = "Value too large";
<p>Enter a number and click OK:</p>
} else {
<input id="id1" type="number" max="100">
txt = "Input OK";
<button onclick="myFunction()">OK</button>
}
<p>If the number is greater than 100 (the
document.getElementById("demo").innerHTML
input's max attribute), an error message will be
= txt;
displayed.</p>
}
<p id="demo"></p>
</script></body></html>
<script>
function myFunction() {
var txt = "";
Example
<!DOCTYPE html> txt = "Value too small";
<html><body> } else {
<p>Enter a number and click OK:</p> txt = "Input OK";
<input id="id1" type="number" min="100"> }
<button onclick="myFunction()">OK</button> document.getElementById("demo").innerHTML =
txt;
<p>If the number is less than 100 (the input's min
attribute), an error message will be displayed.</p> }
<p id="demo"></p> </script></body></html>
<script>
function myFunction() {
var txt = "";
if
(document.getElementById("id1").validity.rangeUnde
rflow) {
JavaScript Objects
</body>
<p id="demo"></p>
</html>
<script>
Example
<!DOCTYPE html> eyeColor : "blue"
<html><body> };
<p>Creating a JavaScript document.getElementById(
Object.</p> "demo").innerHTML =
<p id="demo"></p> person.firstName + " " +
person.lastName;
<script>
</script></body></html>
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
Object Properties
Property Value
firstName John
lastName Doe
age 50
eyeColor blue
document.getElementById("dem
o").innerHTML = some;
}
Example
<!DOCTYPE html> }
<html><body> function myFirst() {
<h2>JavaScript Function myDisplayer("Hello");
Sequence</h2> }
<p>JavaScript functions are function mySecond() {
executed in the sequence they
are called.</p> myDisplayer("Goodbye");
<p id="demo"></p> }
<script> mySecond();
function myDisplayer(some) { myFirst();
</script></body></html>
document.getElementById("dem
o").innerHTML = some;
Example
<!DOCTYPE html> emo").innerHTML = some;
<html><body> }
<h2>JavaScript function myCalculator(num1,
Functions</h2> num2) {
<p>Do a calculation and let sum = num1 + num2;
then display the result.</p> return sum;
<p id="demo"></p> }
<script> let result = myCalculator(5,
function myDisplayer(some) 5);
{ myDisplayer(result);
</script></body></html>
document.getElementById("d
Example
<!DOCTYPE html> "demo").innerHTML =
<html><body> some;
<h2>JavaScript }
Callbacks</h2> function
<p>Do a calculation and myCalculator(num1, num2,
then display the myCallback) {
result.</p> let sum = num1 + num2;
<p id="demo"></p> myCallback(sum);
<script> }
function myCalculator(5, 5,
myDisplayer(some) { myDisplayer);
</script></body></html>
document.getElementById(
Asynchronous JavaScript
document.getElementById("demo").inner
<h2>JavaScript SetTimeout()</h2>
HTML = "I love You !!";
}
<p>Wait 4 seconds (4000 milliseconds),
</script>
and this page will change.</p>
</body>
<h1 id="demo"></h1>
</html>
<script>
Example
<!DOCTYPE html>
<html> function myFunction() {
<body> let d = new Date();
document.getElementById("demo").innerHTML=
<h2>JavaScript setInterval()</h2> d.getHours() + ":" +
d.getMinutes() + ":" +
<p>Using setInterval() to display the time every d.getSeconds();
second (1000 milliseconds).</p>
}
</script>
<h1 id="demo"></h1>
</body>
<script>
</html>
setInterval(myFunction, 1000);
Promise
<!DOCTYPE html> if (x == 0) {
<html> myResolve("OK");
<body> } else {
<h2>JavaScript Promise</h2> myReject("Error");
<p id="demo"></p> }
<script> });
function myDisplayer(some) { myPromise.then(
document.getElementById("demo").innerHTML function(response) {myDisplayer(response);},
= some;
function(error) {myDisplayer(error);}
}
);
let myPromise = new
</script></body></html>
Promise(function(myResolve, myReject) {
let x = 0; // some code (try to change x to 5)
Promise Object Properties
A JavaScript Promise object can be:
Pending
Fullfilled
Rejected
The Promise object supports two properties: state and result.
While a Promise object is "pending" (working), the result is undefined.
When a Promise object is "fullfilled", the result is a value.
When a Promise object is "rejected", the result is an error object.
Syntax:
promise.then(function(result)
{ // if it worked }, { // if it broke }
);
Example
<!DOCTYPE html> if (x == 0) {
<html><body> myResolve("OK");
<h2>JavaScript Promise</h2> } else {
<p id="demo"></p> myReject("Error");
<script> }
function myDisplayer(some) { });
myPromise.then(
document.getElementById("demo").innerH
function(response)
TML = some;
{myDisplayer(response);},
}
function(error) {myDisplayer(error);}
let myPromise = new
);
Promise(function(myResolve, myReject) {
</script></body></html>
let x = 0;
// some code (try to change x to 5)