bcuria
07-21-2002, 04:33 AM
I have an age input field. I want to validate that the age the user enters falls between 18 and 21. How do I do this? I have attached my file. NOTE: The function I am trying to write is called checkAge().
|
||||
evaluate a field for an age rangebcuria 07-21-2002, 04:33 AM I have an age input field. I want to validate that the age the user enters falls between 18 and 21. How do I do this? I have attached my file. NOTE: The function I am trying to write is called checkAge(). ACJavascript 07-21-2002, 06:25 AM Well the way you were going about it was a good way, but in my opinion not the esiest. I like to go the easy way out hehehehe :D So i played with the function and heres what i came up with, it seemed to work for me:D I've boldfaced what i changed <HTML> <HEAD> <TITLE>Assignment 3, Exercise 1</TITLE> <script LANGUAGE="JavaScript"> function clearField(field) { // Check if field contains the default value if (field.value == field.defaultValue) { // It does, so clear the field field.value = ""; } } function checkField(field) { // Check if user has entered information in the field if (field.value == "") { // User has not entered anything field.value = field.defaultValue; } } function checkAge(field) { var age = document.forms[0].age.value; if (age >= 17 && age <=21) { alert("Your Of Age") //Do whatever if the age is correct. }else{ alert("Your Not Of Age") document.forms[0].age.value="Your Age" } } </script> </HEAD> <BODY> <H1>Beverley Curia</H1> <H2>Assignment 3, Week 4, Exercise 1 (341.htm)</H2> <P>Questions? <BR>Send me an email: <A HREF="http://bevcuria@hotmail.com">bevcuria@hotmail.com</A></P> <BR> <BR> <BR> Extend the functionality of 341.htm to check whether the age entered is in a specific numeric range - (ie, at least 18 & less 21) - and, if not, warn the user and return the default value. <BR> <BR> <table border="0" width="95%" cellspacing="0" cellpadding="0"> <tr> <td width="100%" align="center"><form METHOD="POST"> <div align="center"><center><table border="2" cellpadding="5" bgcolor="#FAFAC8"> <tr> <td align="center"><input TYPE="text" NAME="name" VALUE="Name" onFocus="clearField(this);" onBlur="checkField(this);" size="20"></td> </tr> <tr> <td align="center"><input TYPE="text" NAME="email" VALUE="E-mail Address" onFocus="clearField(this);" onBlur="checkField(this);" size="20"></td> </tr> <tr> <td align="center"><input TYPE="text" NAME="age" VALUE="Your Age" onFocus="clearField(this);" onBlur="checkAge(this);" size="20"></td> </tr> </table> </center></div> </form> </td> </tr> </table> </center></div> <HR> <P><CENTER><A HREF="http://www.geocities.com/bevcuria/assign2.html">Return to my Assignment 2 List</A></CENTER></P> </BODY> </HTML> Hope this works for ya :D:D bcuria 07-21-2002, 08:23 AM yes, it did work. I am having a terrible time understanding JavaScript, which is really frustrating because everyone says it's so easy! I am a "procedures-based" type of person, so this is forcing me to think in a non=traditional way. nolachrymose 07-21-2002, 01:16 PM This line: if (age >= 17 && age <=21) { ...should be this: if (age >= 18 && age <=21) { The former allows the user to be between seventeen and twenty-one. Hope that helps! Happy coding! :) ACJavascript 07-21-2002, 04:41 PM OH MAN!!!!!!!!!!!!!!!!!!!! :D:D:D I can't believe i mest that up, hehehehe :D thanks nolachrymose for catchen the mistake :D |
| |||
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum