Top Banner
Using JavaScript with Forms
22
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MWD1001 Website Production Using JavaScript with Forms.

Using JavaScript with Forms

Page 2: MWD1001 Website Production Using JavaScript with Forms.

Topics

JavaScript Basics JavaScript and Forms Dreamweaver Implementation Custom Functions

Page 3: MWD1001 Website Production Using JavaScript with Forms.

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

Page 4: MWD1001 Website Production Using JavaScript with Forms.

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

Page 5: MWD1001 Website Production Using JavaScript with Forms.

Implementation

Page 6: MWD1001 Website Production Using JavaScript with Forms.

Adding JavaScript to pages

JavaScript code goes in <script></script> tags:

<script language="javascript"><!--

function hello (name) {alert("Hello " + name);

}--></script>

Page 7: MWD1001 Website Production Using JavaScript with Forms.

Locations

Typically add code to the <HEAD> of the web page

Can also use separate file:<script language="JavaScript" src="file.js">

Tend to use functions to contain JavaScript code

Need a mechanism to call the functions

Page 8: MWD1001 Website Production Using JavaScript with Forms.

Calling functions

HTML tags can trigger JavaScript events:

<a href="#" onMouseOver="hello('Fred')">

<input type="button" value="Click Me" onClick="hello('fred')">

Different events are valid for different tags – a longer list is on Moodle

Page 9: MWD1001 Website Production Using JavaScript with Forms.

Web Forms

Page 10: MWD1001 Website Production Using JavaScript with Forms.

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

Page 11: MWD1001 Website Production Using JavaScript with Forms.

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

Page 12: MWD1001 Website Production Using JavaScript with Forms.

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

Page 13: MWD1001 Website Production Using JavaScript with Forms.

Sample check

Function checkForm( ) {first =

document.addrForm.firstName.value; if ((first == null) || (first == ""))

then {alert("Please enter first name");return false;

} return true;

}

Page 14: MWD1001 Website Production Using JavaScript with Forms.

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 {

Page 15: MWD1001 Website Production Using JavaScript with Forms.

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

Page 16: MWD1001 Website Production Using JavaScript with Forms.

Other form fields

Radio buttons and check boxes will have a value if selected and null if not

<SELECT> 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

Page 17: MWD1001 Website Production Using JavaScript with Forms.

Using Dreamweaver

Page 18: MWD1001 Website Production Using JavaScript with Forms.

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

Page 19: MWD1001 Website Production Using JavaScript with Forms.

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

Page 20: MWD1001 Website Production Using JavaScript with Forms.

Custom Functions

Page 21: MWD1001 Website Production Using JavaScript with Forms.

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

Page 22: MWD1001 Website Production Using JavaScript with Forms.

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