Presentation is loading. Please wait.

Presentation is loading. Please wait.

MWD1001 Website Production Using JavaScript with Forms.

Similar presentations


Presentation on theme: "MWD1001 Website Production Using JavaScript with Forms."— Presentation transcript:

1 MWD1001 Website Production Using JavaScript with Forms

2 MWD1001 Website Production Topics  JavaScript Basics  JavaScript and Forms  Dreamweaver Implementation  Custom Functions

3 MWD1001 Website Production Basics  JavaScript is an interpreted language which follows C-style syntax  Code can be embedded in web pages or linked from other files  Object-Oriented style – usually event-driven –Mouse over and mouse click events –Form submissions –Page Opening events

4 MWD1001 Website Production Careful!  JavaScript is not Java!  Variables are not typed –Always check user inputs  Code can be seen by visitors to web page  Browser compatibility issues

5 MWD1001 Website Production Implementation

6 MWD1001 Website Production Adding JavaScript to pages  JavaScript code goes in tags: 

7 MWD1001 Website Production Locations  Typically add code to the of the web page  Can also use separate file:  Tend to use functions to contain JavaScript code  Need a mechanism to call the functions

8 MWD1001 Website Production Calling functions  HTML tags can trigger JavaScript events:   Different events are valid for different tags – a longer list is on Moodle

9 MWD1001 Website Production Web Forms

10 MWD1001 Website Production Validation  Common use of JavaScript is to ensure required fields of a form have been filled in  Can go further and insist field contents follow set pattern – eg Post Code or email address  Test the contents of specified form fields to verify their contents

11 MWD1001 Website Production Implementation  Use the onSubmit parameter of a form tag to trigger JavaScript event  Call a function to validate form and return either true or false  e.g. <form onSubmit="return validate( )"...  Browser will only submit form when a return value of true received

12 MWD1001 Website Production Variable Names  Important to give each form a unique name parameter –Assume a form called addrForm –Assume textbox called firstName  Can refer to value typed into the first name text box as: –document.addrForm.firstName.value

13 MWD1001 Website Production Sample check  Function checkForm( ) { first = document.addrForm.firstName.value;  if ((first == null) || (first == "")) then { alert("Please enter first name"); return false; }  return true; }

14 MWD1001 Website Production Further Checks  May want to check other field more closely –Email address must contain @ sign  Use JavaScript built-in indexOf() function  Returns position of @ character in string or - 1 if not found  if (email.indexOf('@') < 0) then {

15 MWD1001 Website Production Basic validation  Build a series of If statements to check whether fields are missing or invalid  Return false if a field is missing  Have a final return true if all tests passed

16 MWD1001 Website Production Other form fields  Radio buttons and check boxes will have a value if selected and null if not  dropdowns are more complex  Treated as an array – so we need an array index  s = document.frm.selectBox; val = s.options[s.selectedIndex].value;  Rarely need to validate drop-down menus

17 MWD1001 Website Production Using Dreamweaver

18 MWD1001 Website Production Canned JavaScript  Dreamweaver allows you to add built-in JavaScript functions to your pages –Click on a form –Open the behaviors tab –Click on the + –Pick Validate Form –Choose the fields you need

19 MWD1001 Website Production Dreamweaver Functions  The canned functions in Dreamweaver are generic  In many cases they are good enough  Code is much more involved than the simple examples you have seen so far  May find the error messages are not friendly enough

20 MWD1001 Website Production Custom Functions

21 MWD1001 Website Production Patterns in Data Fields  What is a valid email address?  What is a valid UK postcode?  What is a valid credit card number?  Can obtain JavaScript functions from the Internet to validate these data types

22 MWD1001 Website Production Validating Credit Cards  Credit card numbers are generated according to a mathematical algorithm  Can be checked for validity using the Luhn algorithm –Starting from the right-hand side – double the second (and alternate) digits –If number > 10, add two digits together –Add all numbers (doubled and not) together –Should be a multiple of 10


Download ppt "MWD1001 Website Production Using JavaScript with Forms."

Similar presentations


Ads by Google