360 likes | 376 Views
Learn about JavaScript logical structures, including conditional statements and operators. Explore examples and understand how to process inputs effectively. Practice form validation and enhance your web programming skills.
E N D
CSC317 – INTERNET PROGRAMMINGCSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
OutlineBasic JavaScript Syntax (recap) Variable (recap) JavaScript Functions (recap) Parsing User Input (recap) Logical structures How to Process Inputs Form Validation Page 2
Basic JavaScriptComparison Operators Comparison operators (the following examples will used x = 5) Page 3
Basic JavaScriptLogical Operators Logical operators determine logic between values (the following examples will used x = 6and y = 3) Page 4
Basic JavaScriptLogical Structures • Your application may involves conditions • Example • If male, he wears “bajumelayu” • If female, she wears “bajukurung” • JavaScript has logical structures (conditional statements), where you need to use operators on the previous two slides Page 5
Basic JavaScriptConditional Statements Used to perform different actions based on different conditions/decisions • if statement execute some code only if a specified condition is true • if..else statement execute some code if the condition is true and another code if the condition is false • if..else if...elsestatement select one of many blocks of code to be executed • switch statement select one of many blocks of code to be executed Page 6
Basic JavaScriptLogical Structures if(male){ document.write(“He wears bajumelayu”); } else{ document.write(“She wears bajukurung”); } switch(gender){ case “male” : case “MALE”: alert(“He wears bajumelayu”); break; case “female”: alert(“She wears bajukurung”); break; default : alert(“Please select gender!”); } Page 7 Simple examples
Basic JavaScriptLogical Structures if(condition){ // execute the code if condition is true } if(place == “Merbok”){ document.write(“It is situated in Kedah”); } if(university == “UiTM” && place == “Merbok”){ document.write(“It is situated in Kedah”); } ifstatement: execute some code if a specified condition is true Page 8
Basic JavaScriptLogical Structures if(program == “CS113” || program == “CS110”){ document.write(“These are UiTM programs”); } if(time >= 1 && time < 12){ document.write(“Good Morning!”); } if statement: execute some code if a specified condition is true Page 9
Basic JavaScriptLogical Structures if(condition){ // execute the code if condition is true } else{ // execute the code if condition is false } if…else statement: execute some code if a specified condition is true, and another code if the condition is false Page 10
Basic JavaScriptLogical Structures if(gender == “Male”){ document.write(“Go to Hall A”); } else{ document.write(“Go to Hall B”); } if…else statement: execute some code if a specified condition is true, and another code if the condition is false Page 11
Basic JavaScriptLogical Structures if(condition1){ // execute code if condition1 is true } else if(condition2){ // execute code if condition2 is true } else if(condition3){ // execute code if condition3 is true } else{ // execute code if all conditions are false } Page 12 if…else if…elsestatement: use this statement if you want to select one of many blocks of code to be executed
Basic JavaScriptLogical Structures if(program == “AS120”){ document.write(“Diploma in Science”); } else if(program == “CS110”){ document.write(“Diploma in Computer Science”); } else if(program == “AC110”){ document.write(“Diploma in Accountancy”); } else{ document.write(“Please select program code”); } Page 13 if…else if…elsestatement: use this statement if you want to select one of many blocks of code to be executed
Basic JavaScriptLogical Structures switch(var){ case 1: // execute code block 1 break; case 2: // execute code block 2 break; // break the loop and continue executing the code that follows after the loop (if any). default: //execute code if var is different // from case 1, 2, 3 } Page 14 switch()statement: use this statement if you want to select one of many blocks of code to be executed
Basic JavaScriptLogical Structures switch(month){ case 1: document.write(“January”); break; case 2: document.write(“February”); break; case 3: document.write(“March”); break; default: document.write(“None of the above”); } Page 15 switch()statement example:
Basic JavaScriptHow to Process Inputs • 1 input field, which a text field, name num1 • 1 input field, which a text field, namenum2 • 1 drop down list, name operator • 1Submitbutton • Supposedly, if user input value within the input fields and select a value in the dropdown list, then click the Submit button, the value from num1&num2 will be processed • create an event that will call a JavaScript function first • that event can be placed at the Submit button Page 16
Basic JavaScriptHow to Process Inputs Page 17 Create a complete HTML form with several input fields [ Your previous coding in Question 5 ]
Basic JavaScriptHow to Process Inputs <script type=“text/javascript”> function kira(){ // your JavaScript code is here } </script> Now, we create the JavaScript user-defined function first, which will be placed at <head>…</head>tag Every time you create user-defined function, it must starts with function followed by function_name, brackets (()), and braces ({}) For this example, create a function name kira() Page 18
Basic JavaScriptHow to Process Inputs varfname = document.{form_name}.{field_name}.value; varnum1 = document.f1.num1.value; var num2 = document.f1.num2.value; var operator = document.f1.operator.value; To capture value or data from input field, we have to write this code Now, place the code above in the kira(),where it would be like this Page 19
Basic JavaScriptHow to Process Inputs <script type="text/javascript"> function kira(){ var num1 = document.f1.num1.value; var num2 = document.f1.num2.value; var operator = document.f1.operator.value; } </script> <input type=“button" name="submit" value="Submit" onclick=“kira()"> Now, add onclick event on the Submit button to callkira() Page 20
Basic JavaScriptHow to Process Inputs We can also validate/check numeric input using isNaN() Now, add this code in function kira()before after calling the input from form f1. Page 22
Basic JavaScriptHow to Process Inputs Your BMI is 19.53 and you are Normal Page 24 Create a complete HTML form with several input fields [ Your previous coding in Question 3 ] Using JavaScript codes, retrieve height and weight values from HTML form. Next, proceed with BMI calculation and determine the category of BMI according to BMI value.
Page 25 Based on figure below, create a JavaScript function that able to check your grade according to your mark. Your JavaScript function must be able to accept an input from HTML form, which enters by user. Next, user will click on Check Grade button to check his/her grade. Display the grade in the text field in HTML form. Please follow conditions in the Table 1, in order to check the grade.
Basic JavaScriptHow to Process Inputs Page 26 Write the JavaScript codes for each of the following: An increment of salary for UPeM lectures based on the following table: Write a complete JavaScript program to calculate and didplay a new salary for UPeM lecturers using an alert box. A program should prompt the user to enter basic salary and grade. Please notify user if they enter invalid grade. Define the calcNewSal() function to calculate the new salary where the function will receives salary increment and increment percentage an return a new salary.
Basic JavaScriptHow to Process Inputs Page 27 Write the JavaScript codes for the following question: • A switch statement that, given a number representing a TV channel, it displays the name of the station that corresponds to that number, or displays some message indicating that the channel is not used. Capture the input using prompt box and output the result using alert box. Use the following channel numbers and the station names: 102:TV2 104:Astro Ria 105:Astro Prima 107:NTV7 114:TVAIHijrah 305:TVB Classic Channel
Form EnhancementEror Checking • Why we have to perform error checking / input verification? • To avoid any (unnecesary|invalid) (character|input) from being accepted according to defined (pattern|format) • Sometimes, each input has its own pattern or format that will determine that input is valid or invalid • Examples: • Email address verification • Name verification • Phone number verification Page 28
Form EnhancementEror Checking • Write a JavaScript function name checkPassword() that will check passwords entered by user and display status of the passwords. Please refer to Figure 3 for your interface : Page 29
Form EnhancementEror Checking no value being entered, undefined value, numeric value <html> <head> <script type="text/javascript"> function cap(){ varfname = document.f1.fname.value; alert("Name: " + fname); } </script> </head> <body> <form name="f1"> Name: <input type="text" name="fname"> <input type=“button“ value="Submit" onclick=“cap()"> </form> </body> </html> Page 30
Form EnhancementEror Checking no value being entered, undefined value, numeric value <script type="text/javascript"> function cap(){ varfname = document.f1.fname.value; alert("Name: " + fname); } </script> • cap()will capture a value from fname text field. This field is specifically for people´s name, which is string value • What will happen if user enter numeric value? • Shouldcap()accept the value without need to do error checking? • What will happen if user does not enter any value? The field leave blank Page 31
Form EnhancementEror Checking no value being entered, undefined value, numeric value <script type="text/javascript"> function cap(){ varfname = document.f1.fname.value; if(fname== ""){ // check if no value being entered alert("Please enter a name!"); } else if(fname == null){ // check if there is undefined value alert("Please enter a name!"); } else if(!isNaN(fname)){ // check if value is numeric alert("Invalid character. Name must be alphabet."); } else{ // all conditions above false document.write("Name: " + fname); } } </script> Page 32
Form EnhancementEror Checking fname = ″″ andfname = null are two different statements Page 33
Form EnhancementEror Checking Required Field <html> <head> <script> function validateForm() { var x=document.forms.myForm.fname.value; if (x==null || x=="") { alert("First name must be filled out"); return false; } } </script> </head> <body> <form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post"> First name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> </body> </html> • 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: Page 34 http://www.w3schools.com/js/js_form_validation.asp
Form EnhancementEror Checking E-mail Validation <html> <head> <script> 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; } } </script> </head> <body> <form name="myForm" action=“" onsubmit="return validateForm()" method="post"> Email: <input type="text" name="email"> <input type="submit" value="Submit"> </form></body></html> • 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: Page 35 http://www.w3schools.com/js/js_form_validation.asp
Introduction to Client-Side Scripting Bibliography (Book) Knuckles (2001). Introduction to Interactive Programming on the Internet using HTML & Javascript. John Wiley & Sons, Inc. Bibliography (Websites) http://www.w3schools.com/js/default.asp http://www.w3schools.com/js/js_form_validation.asp http://webcheatsheet.com/javascript/form_validation.php Page 36