Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    San Jose
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    evaluate a field for an age range

    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().
    Attached Files Attached Files

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    FL, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


    So i played with the function and heres what i came up with, it seemed to work for me

    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
    CYWebmaster.com - See why we dot com!!
    ACJavascripts.com - Cut & Paste Javascripts!
    SimplyProgram.com - Personal Blog

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    San Jose
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks!

    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.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This line:

    Code:
    if (age >= 17 && age <=21) {
    ...should be this:

    Code:
    if (age >= 18 && age <=21) {
    The former allows the user to be between seventeen and twenty-one.

    Hope that helps!

    Happy coding!

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    FL, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OH MAN!!!!!!!!!!!!!!!!!!!!

    I can't believe i mest that up, hehehehe

    thanks nolachrymose for catchen the mistake
    CYWebmaster.com - See why we dot com!!
    ACJavascripts.com - Cut & Paste Javascripts!
    SimplyProgram.com - Personal Blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •