Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript.

Similar presentations


Presentation on theme: "JavaScript."— Presentation transcript:

1 JavaScript

2 Java Script Date Object
Created with the new Date( ) Methods allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object constructor new Date( ) new Date(milliseconds) new Date(datestring) new Date(year,month,date[,hour,minute,second,millisecond ])

3

4 JavaScript Form Validation
validate the form submitted by the user because it can have inappropriate values can validate name, password, , date, mobile number etc fields provides you the facility the validate the form on the client side so processing will be fast than server-side validation

5 Date Objects <html> <body> current time: <p id="time"></p> <script> window.onload=function(){gettime();} function gettime() { var a=new Date(); var h=a.getHours(); var m=a.getMinutes(); var s=a.getSeconds(); m=checktime(m); s=checktime(s); document.getElementById("time").innerHTML=h+":"+m+":"+s; setTimeout(function(){gettime()},1000); } function checktime(i) { if(i<10) i="0"+i; return i; </script> </body> </html>

6 JavaScript-Regular Expression
A regular expression is an object that describes a pattern of characters. used to perform pattern-matching and "search-and-replace" functions on text Syntax /pattern/modifiers; Example var patt = /w3schools/i /w3schools/i  is a regular expression. w3schools  is a pattern (to be used in a search). i  is a modifier (modifies the search to be case-insensitive).

7 Modifiers perform case-insensitive and global searches Modifier Description i Perform case-insensitive matching g Perform a global match (find all matches rather than stopping after the first match) m Perform multiline matching Brackets used to find a range of characters

8 Metacharacters

9 <html> <body> <p>Click the button to do a global search for word characters in a string.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var str = "Give 100%!"; var patt1 = /\w/g; var result = str.match(patt1); document.getElementById("demo").innerHTML = result; } </script> </body> </html>

10 JavaScript - Errors & Exceptions Handling
Three types of errors in programming: Syntax Errors Runtime Errors Logical Errors try...catch...finally Statement The try statement -test a block of code for errors. The catch statement -handle the error. The throw statement -create custom errors. The finally statement -execute code, after try and catch, regardless of the result.

11 <html> <head> <script type="text/javascript"> function myFunc() { var a = 100; try { alert("Value of variable a is : " + a ); } catch ( e ) { alert("Error: " + e.description ); </script> </head> <body> <p>Click the following to see the result:</p> <form> <input type="button" value="Click Me" onclick="myFunc();" /> </form> </body> </html>

12

13 <script type="text/javascript"> function myFunc() { var a = 100;
</head> <body> <p>Click the following to see the result:</p> <form> <input type="button" value="Click Me" onclick="myFunc();" /> </form> </body> </html> <html> <head> <script type="text/javascript"> function myFunc() { var a = 100; var b = 0; try{ if ( b == 0 ){ throw( "Divide by zero error." ); } else var c = a / b; catch ( e ) { alert("Error: " + e ); </script>

14

15 JavaScript Form Validation
to validate the form submitted by the user validate the form on the client side so processing will be fast than server-side validation.

16 html> <script> function validateform(){ var name=document.myform.name.value; var password=document.myform.password.value; if (name==null || name==""){ alert("Name can't be blank"); return false; }else if(password.length<6){ alert("Password must be at least 6 characters long."); } </script> <body> <form name="myform" method="post“ onsubmit="return validateform()" > Name: <input type="text" name="name"><br/> Password: <input type="password" name="password"><br/> <input type="submit" value="register"> </form> </body> </html>

17

18 <html> <script type="text/javascript"> function matchpass(){ var firstpassword=document.f1.password.value; var secondpassword=document.f1.password2.value; if(firstpassword==secondpassword){ return true; } else{ alert("password must be same!"); return false; </script> <body> <form name="f1" onsubmit="return matchpass()"> Password:<input type="password" name="password" /><br/> Re-enter Password:<input type="password" name="password2"/><br/> <input type="submit"> </form> </body> </html>

19

20 JavaScript Built in Objects
In JavaScript, almost "everything" is an object. Booleans can be objects (if defined with the new keyword) Numbers can be objects (if defined with the new keyword) Strings can be objects (if defined with the new keyword) Dates are always objects Maths are always objects Regular expressions are always objects Arrays are always objects Functions are always objects Objects are always objects


Download ppt "JavaScript."

Similar presentations


Ads by Google