Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Simple Javascript help!! :(

Simple Javascript help!! :(
Thread Tools
badtz
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Sep 11, 2003, 06:24 PM
 
script type="text/javascript">
function validate()
{
x=document.myForm
txt=x.myInput.value
if (txt=1 && txt<=5)
{
alert("The New York Times is deliverable to your area! ")
return true
}
else
{
alert("Unfortunately, the New York Times doesn't deliver to your area ")
return false
}
}
</script>


<form name="myForm" onsubmit="return validate()">

Enter Zipcode:
<input type="text" name="myInput">

<input type="submit" value="Check Zipcode">

</form>
I'm currently using this javascript to pull up a simple pop-up message stating if the entered zip code is correct or incorrect from a list of zipcodes I have.


Questions ::

1. How can I list my own numbers[list values] instead of using the numbers between 1 and 5 as stated on the script? I've tried editing it, but to no avail!

2. Is there anyway for the form to NOT submit the data, but just provide a pop-up window. Currently, the URL changes according to the value from the form (i'm going to embed this into a webpage later, so I don't want it to mess anything up).


Thanks so much for any help! !!!!
     
pumpkinapo
Junior Member
Join Date: Aug 2000
Location: Califon, NJ
Status: Offline
Reply With Quote
Sep 12, 2003, 10:57 AM
 
Originally posted by badtz:
1. How can I list my own numbers[list values] instead of using the numbers between 1 and 5 as stated on the script? I've tried editing it, but to no avail!
Do you mean like a drop down or mutiple select box?
Peter Leing
17" 1.67GHz PowerBook
450MHz G4 Cube
     
Ver de Terre
Forum Regular
Join Date: Feb 2003
Status: Offline
Reply With Quote
Sep 12, 2003, 12:28 PM
 
I'm not that well versed in JavaScript, and there may be a more elegant solution, but try this:

[php]<script language="javascript">
<!--
function validate() {
var input_value = document.theform.theinput.value;
var good_values_list = new Array(123, 132, 213, 231, 312, 321);
var found = false;
var i;
for (i = 0; i < good_values_list.length; i++) {
if (good_values_list[i] == input_value) {
found = true;
break;
}
}
alert("In list: " + found);
return found;
}
-->
</script>[/php]

[php]<form action="WHATEVER.cgi" onsubmit="return validate();" name="theform">
<input type="text" name="theinput">
<input type="submit">
</form>[/php]

Replace your values in the array. When the onsubmit handler in the form returns false, the form action is canceled. If you want just the alert box whether the input is good or not, use onsubmit="validate();return false;"
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 11:04 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,