Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-21-2002, 04:33 AM   PM User | #1
bcuria
New Coder

 
Join Date: Jul 2002
Location: San Jose
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
bcuria is an unknown quantity at this point
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
File Type: txt 341.txt (2.1 KB, 120 views)
bcuria is offline   Reply With Quote
Old 07-21-2002, 06:25 AM   PM User | #2
ACJavascript
Regular Coder

 
Join Date: Jun 2002
Location: FL, USA
Posts: 734
Thanks: 0
Thanked 0 Times in 0 Posts
ACJavascript is on a distinguished road
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
ACJavascript is offline   Reply With Quote
Old 07-21-2002, 08:23 AM   PM User | #3
bcuria
New Coder

 
Join Date: Jul 2002
Location: San Jose
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
bcuria is an unknown quantity at this point
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.
bcuria is offline   Reply With Quote
Old 07-21-2002, 01:16 PM   PM User | #4
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
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!
nolachrymose is offline   Reply With Quote
Old 07-21-2002, 04:41 PM   PM User | #5
ACJavascript
Regular Coder

 
Join Date: Jun 2002
Location: FL, USA
Posts: 734
Thanks: 0
Thanked 0 Times in 0 Posts
ACJavascript is on a distinguished road
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
ACJavascript is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:44 AM.


Advertisement
Log in to turn off these ads.