Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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. David C. Gibbs 2003-04 NOTE: click on the “Slide Show” icon in the lower right of the screen to hear the audio track

3 WDMD 170 – UW Stevens Point 2 Tutorial 6 Forms Section B - Validating a User's Input to a Form

4 WDMD 170 – UW Stevens Point 3 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

5 WDMD 170 – UW Stevens Point 4 Hidden Form Fields Special type of form element that allows the hiding of information from users Created with tag, setting the TYPE attribute to hidden Can be used to store information the program needs later

6 WDMD 170 – UW Stevens Point 5 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.htmlCalculator.html

7 WDMD 170 – UW Stevens Point 6 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 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

8 WDMD 170 – UW Stevens Point 7 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.

9 WDMD 170 – UW Stevens Point 8 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]

10 WDMD 170 – UW Stevens Point 9 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].value

11 WDMD 170 – UW Stevens Point 10 onSubmit –Executes when a form is submitted to a CGI script using a submit or an image input tag onReset –Executes when a reset button is selected on a form We saw these used in Tutorial 06A. Of course, both can be used in the same form tag. tag: event handlers

12 WDMD 170 – UW Stevens Point 11 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.

13 WDMD 170 – UW Stevens Point 12 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.htmvalidateNumericField.htm 2.validatePasswordLength.htmvalidatePasswordLength.htm 3.validateEmailAddress.htmvalidateEmailAddress.htm These examples are examined in detail in the next several slides. eTask 2

14 WDMD 170 – UW Stevens Point 13 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

15 WDMD 170 – UW Stevens Point 14 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.

16 WDMD 170 – UW Stevens Point 15 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 otherwise 3.var myValue = myField.value 4.if(isNaN(myValue) || myValue == ""){ 5. alert("You must enter a valid number!"); 6. myField.value = ""; 7. return false; 8.} else 9. return true; 10.} Code sample: validateNumericField.htmvalidateNumericField.htm

17 WDMD 170 – UW Stevens Point 16 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. Anything more than 10 is ignored by the code using the text field. But - the minimum length must be validated.

18 WDMD 170 – UW Stevens Point 17 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 < len 3. 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. } else 9. return true; 10.} Code sample: validatePasswordLength.htmvalidatePasswordLength.htm

19 WDMD 170 – UW Stevens Point 18 Validating User Input to a Form: an email address A valid email address is in the form a@b.ca@b.c 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!

20 WDMD 170 – UW Stevens Point 19 Validating an email address: regular expressions JavaScript contains a powerful technique available in most programming languages – regular expressions.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.

21 WDMD 170 – UW Stevens Point 20 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 true 5. 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.htmvalidateEmailAddress.htm

22 WDMD 170 – UW Stevens Point 21 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.htmvalidateEmailAddress.htm

23 WDMD 170 – UW Stevens Point 22 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.htmvalidateEmailAddress.htm

24 WDMD 170 – UW Stevens Point 23 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”

25 WDMD 170 – UW Stevens Point 24 E-Mailing Form Data: example <form action = "mailto:president@whitehouse.gov" 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:president@whitehouse.gov?subject=Iraq?cc=vicepresident@whitehouse.gov This fills in the Subject: and Cc: fields in most mail programs. Code sample: eMailingFormData.htmeMailingFormData.htm

26 WDMD 170 – UW Stevens Point 25 Result of using “mailto:”

27 WDMD 170 – UW Stevens Point 26 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

28 WDMD 170 – UW Stevens Point 27 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.

29 WDMD 170 – UW Stevens Point 28 End of eLesson Jump to the Beginning of this eLesson WDMD 170 Course Schedule D2L Courseware Site


Download ppt "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."

Similar presentations


Ads by Google