28
1 WDMD 170 – UW Stevens Point WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr. David C. Gibbs 2003-04 NOTE: click on the “Slide Show” icon in the lower right of the screen to hear the audio track

WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

Embed Size (px)

Citation preview

Page 1: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

1WDMD 170 – UW Stevens Point

WDMD 170 Internet Languages

eLesson: Working with Forms in JavaScript(there is an audio component to this eLesson)

© Dr. David C. Gibbs2003-04

NOTE: click on the “Slide Show” icon in the lower right of the screen to hear the audio track

Page 2: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

2WDMD 170 – UW Stevens Point

Tutorial 6

Forms

Section B - Validating a User's Input to a Form

Page 3: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

3WDMD 170 – UW Stevens Point

Tutorial 6B Topics

• Section B - Validating a User's Input to a Form– About hidden form fields– About the Form object– How to reference forms and form

elements– About form event handlers, methods, and

properties– How to e-mail form data

Page 4: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

4WDMD 170 – UW Stevens Point

Hidden Form Fields

• Special type of form element that allows the hiding of information from users

• Created with <input> tag, setting the TYPE attribute to hidden

• Can be used to store information the program needs later

Page 5: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

5WDMD 170 – UW Stevens Point

Hidden Form Fields: the calculator example revised

Extends the calculator of Tutorial 3 by adding the following:

1. Adds the button M+ – which adds the displayed value to memory

2. Adds the button MRC – which recalls the stored memory value

3. Adds the button MC – which clears the memory

4. Implements the “memory” cell as a hidden form field.

Code sample: Calculator.html

Page 6: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

6WDMD 170 – UW Stevens Point

• Copy the source code for Calculator.html found in Tutorial 06 into your working Tutorial06 folder.

• Create an XHTML page to explain the features of the new Calculator.

– Provide a link to the calculator– Copy in the text of items 1-4 from the previous slide,

and beneath each one (between <pre> tags), add the actual HTML code for each additional item. For example, here is the third one:

3. Adds the button MC – which clears the memory. Code: <input type="button" name="memClear" value="MC"

onclick="document.Calculator.storedValue.value=0">

– Then explain what the code actually does. – Save your file as ModifiedCalculatorExplained.htm.

eTask 1

Page 7: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

7WDMD 170 – UW Stevens Point

The Form Object

• Enables the use of JavaScript for verifying form information.– Allows validation before submission to

server• Minimizes Web traffic• Simplifies server-side scripting, because only

“good” data is passed

• This is called “client-side” validation – as opposed to “server-side” validation.

Page 8: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

8WDMD 170 – UW Stevens Point

The Form Object:Referencing Forms and Form

Elements• Document object includes a forms[] array

that contains all of an HTML document’s forms

• Like any array, subscripted [0] thru [n-1]• Form object includes an elements[] array

that contains all of a form’s elements• Placed into array in order they are placed

in form • To reference third element in second form:

– document.forms[1].elements[2]

Page 9: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

9WDMD 170 – UW Stevens Point

<form> tag: NAME attribute

• Allows JavaScript to reference the item (e.g., form, element, etc.)

• If multiple form elements share the same name, JavaScript creates an array of those elements– Radio buttons– document.demographics.ageGroup[1].v

alue

Page 10: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

10WDMD 170 – UW Stevens Point

• onSubmit– Executes when a form is submitted to a CGI

script using a submit or an image input tag<form name="myForm" onSubmit="anotherPage.htm">

• onReset– Executes when a reset button is selected on a

form<form name="myForm" onReset="return confirmReset();">

We saw these used in Tutorial 06A.Of course, both can be used in the same

form tag.

<form> tag: event handlers

Page 11: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

11WDMD 170 – UW Stevens Point

Validating User Input to a Form: individual form elements

• Form elements we will validate– A numeric field– The length of a password field– The validity of an email address

• You will implement these validation techniques in your project.

Page 12: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

12WDMD 170 – UW Stevens Point

• Actually, this is a “hands-on” task.

• Load each of the validation examples given below into HTML-Kit and print them. [That way you can study them independent of the screen!]

1. validateNumericField.htm

2. validatePasswordLength.htm

3. validateEmailAddress.htm

• These examples are examined in detail in the next several slides.

eTask 2

Page 13: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

13WDMD 170 – UW Stevens Point

Validating User Input to a Form:a numeric field

• Makes use of the JS function isNaN() which returns true if the value passed is “Not a Number”.

• Copy the following code into an html page and view the page:alert("5 is: " + isNaN(5));alert("Q is: " + isNaN("Q"));

• What is returned by isNaN(5)? Ans: false• What is returned by isNaN("Q")? Ans: true

Page 14: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

14WDMD 170 – UW Stevens Point

Validating User Input to a Form:a numeric field

• Caution: the function isNaN() applied to the empty string (“”) returns false – which means it’s a number? Copy this into an html page script tag: alert(isNaN(""));

and see what is displayed when you execute the script!

• So you must test for the empty string as well.

Page 15: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

15WDMD 170 – UW Stevens Point

Validating a numeric field

The function to check for a numeric field:

1. function validNumeric(myField){ 2. // returns true if myField is a number; false otherwise3. var myValue = myField.value4. if(isNaN(myValue) || myValue == ""){5. alert("You must enter a valid number!");6. myField.value = "";7. return false;8. } else9. return true;10. }

Code sample: validateNumericField.htm

Page 16: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

16WDMD 170 – UW Stevens Point

Validating User Input to a Form:length of a password field

• Many passwords must be a certain minimum and maximum length.

• Text fields (i.e. <input type=“text”… ) can have a maxlength attribute.

<input type="password" maxlength="10">

Anything more than 10 is ignored by the code using the text field.

• But - the minimum length must be validated.

Page 17: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

17WDMD 170 – UW Stevens Point

Validating the length of a password

To check the minimum length, reference the .length attribute of the value.

1. function checkStrLength(txtBox, len) { 2. // returns false if length of the txtbox field < len3. if (txtBox.value.length < len){4. alert('Your password must be at least ' + len + '

characters long');5. txtBox.value="";6. txtBox.focus();7. return false;8. } else9. return true;10. }

Code sample: validatePasswordLength.htm

Page 18: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

18WDMD 170 – UW Stevens Point

Validating User Input to a Form:an email address

A valid email address is in the form [email protected]

A validation routine would verify 5 pieces:1. a string of one or more characters, followed by,2. a lower case “at-sign”, @, followed by,3. a string of one or more characters, followed by,4. a period “.”, followed by,5. a string of one or more characters.

Doing this with string methods would be a nightmare!

Page 19: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

19WDMD 170 – UW Stevens Point

Validating an email address: regular expressions

• JavaScript contains a powerful technique available in most programming languages – regular expressions.

• A regular expression is a pattern used to match character combinations in strings.

• We will use a prepared function that uses regular expressions to validate email addresses.

• You only need to be able to invoke the function from within your code.

Page 20: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

20WDMD 170 – UW Stevens Point

Validating using regular expressions

The regular expression is in line 2. If you look closely, you can find the mandatory “@” and “.” characters. Line 3 compares the value to be checked against the “filter”.

1. function validEmailAddr(checkMe) {

2. var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;

3. if (filter.test(checkMe)) 4. return true5. else {6. return false;7. }8. }

Again, the good news is at this stage all you have to do is USE it! Refer to the sample code above for implementation details!

Code sample: validateEmailAddress.htm

Page 21: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

21WDMD 170 – UW Stevens Point

Validatation: putting it all together

<form name="frmInfo" onSubmit="validateDataFields();"

onReset='return confirm("Are you sure you wish to reset the form?");'>

Code sample: validateEmailAddress.htm

Page 22: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

22WDMD 170 – UW Stevens Point

Validatation: putting it all together

1. function validateDataFields() {2. var goodStr = checkStrLength(document.frmInfo.txtUsername, 6);3. var goodNumber = validNumeric(document.frmInfo.txtAge);4. var goodEmail = checkEmailAddress(document.frmInfo.txtEmailAddr);5. 6. if ((goodStr == true) && (goodNumber == true) &&

(goodEmail == true)) {7. alert("Good data! Submitting now!");8. 9. }10. }

Code sample: validateEmailAddress.htm

Page 23: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

23WDMD 170 – UW Stevens Point

E-Mailing Form Data

• Instead of submitting data to a server, it can be transferred to an e-mail address– Much simpler than creating scripts on the

server– Recipient must then process information

• Use ENCTYPE = “text/plain”• In the form’s ACTION attribute use

“mailto:email_address”

Page 24: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

24WDMD 170 – UW Stevens Point

E-Mailing Form Data: example

<form action = "mailto:[email protected]"

method = "post" enctype="text/plain" name="voter_information">

Note that the mailto: value has some interesting additions to tweak the mail header fields (see Musciano page 172 for more information).

mailto:[email protected][email protected]

This fills in the Subject: and Cc: fields in most mail programs.

Code sample: eMailingFormData.htm

Page 25: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

25WDMD 170 – UW Stevens Point

Result of using “mailto:

Page 26: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

26WDMD 170 – UW Stevens Point

Summary

Validating User Input within Forms in JavaScript– Hidden form fields; how they work, how they

are used– The Form object, its attributes, and how to

reference forms and form elements– Form event handlers, methods, and properties– Validating input: numeric fields, string length,

email addresses– How to e-mail form data

Page 27: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

27WDMD 170 – UW Stevens Point

Assignment• Complete exercise #7 on page 361 of Gosselin, Tutorial 06B. The text

of the exercise is copied in here, with additions:

Create an RSVP form for a party you are hosting. Your guests should fill out the form supplying the following: their name, phone number, and the number of people coming with them, and submit it to your email address. Your code should verify that the number of people field is genuinely a number. If it is not, present an alert message informing them so. Save the HTML document as RSVP.htm in your Tutorial06 folder.

• Post your solution for exercise #7 to your Tutorial06 folder on the CISSRV3 server.

• Continue working on the form for your project. Add validation one field at a time, starting with a numeric field, then adding the length of a password field, and then validate an email address.

• Add a link in the discussion forum for your project’s form along with any questions or problems you experienced.

• You may also post any questions (and the exercise file) to the eReview group – for discussion purposes.

Page 28: WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr

28WDMD 170 – UW Stevens Point

End of eLesson

• Jump to the Beginning of this eLesson

• WDMD 170 Course Schedule

• D2L Courseware Site