•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,763 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,688 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 8781 | Replies: 13
![]() |
•
•
Join Date: Dec 2006
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
I have a little problem. I have a drop down menu that contains various different entries. I am allowing the user to also have a text box right next to the drop down menu where they can specify a category (and it will insert it into the drop down menu for the next time). The text box, though is an issue for me.
The issue is, if the user types in an entry and the entry already excists in the drop down menu, I want a popup to say "Category already excists" or something like that. It's for validation, but I am wanting the javascript to check the drop down menu so that the same categories aren't entered multiple times.
Is there a way in javascript i can validate these so this does not happen. Thanks.
The issue is, if the user types in an entry and the entry already excists in the drop down menu, I want a popup to say "Category already excists" or something like that. It's for validation, but I am wanting the javascript to check the drop down menu so that the same categories aren't entered multiple times.
Is there a way in javascript i can validate these so this does not happen. Thanks.
Yes, you could use a for loop to iterate through each item in the array of items in the list box. Each time, look to see if that particular item equals the contents of the textbox. If it does, stop and give an error message. But if the loop completes, then allow the contents of the textbox to be added to the listbox and continue on with your script.
•
•
Join Date: Dec 2006
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
Alright, having some issues
.
Here's what I have so far. Here's the form:
[html]
<form method="post" action="doit.php" name="content">
<p><label id="datBegin">Date</label><br />
<input type="text" id="date" name="date" value="<?php echo date('F d, Y'); ?>" />
<input type="hidden" id="time" name="time" value="<?php date('H:i:s'); ?>" /></p>
<p><label id="category">Category</label><br />
<select id="category" name="category" onchange="changeNewCat()">
<optgroup label="Please Pick">
<option value="0">No Category Selected</option>
<?php
$i=0;
while ($i<$num) {
$row = mysql_fetch_assoc($result);
echo "<option value=".$row['category'].">".$row['category']."</option>";
$i++;
}
?>
</optgroup>
</select> OR
<input type="text" id="newcat" name="newcat" /> (Add a new category)</p>
<p><label id="daily">Content</label><br />
<textarea id="daily" name="daily"></textarea></p>
<input type="submit" value="Create Daily" onclick="return checkForm(this.form)" />
</form>
[/html]
First off, don't worry about the PHP. This all works. Also, the
Let me give you javascript before I go further into this:
The javascript just doesn't work. Theres is more to this. Also, don't worry about the part that says
Am I not iterating properly. If someone could guide me or show me a little better way of getting this done, please do show.
I believe this is the last part of the validation of this form and it will be complete.
Any thoughts?
.Here's what I have so far. Here's the form:
[html]
<form method="post" action="doit.php" name="content">
<p><label id="datBegin">Date</label><br />
<input type="text" id="date" name="date" value="<?php echo date('F d, Y'); ?>" />
<input type="hidden" id="time" name="time" value="<?php date('H:i:s'); ?>" /></p>
<p><label id="category">Category</label><br />
<select id="category" name="category" onchange="changeNewCat()">
<optgroup label="Please Pick">
<option value="0">No Category Selected</option>
<?php
$i=0;
while ($i<$num) {
$row = mysql_fetch_assoc($result);
echo "<option value=".$row['category'].">".$row['category']."</option>";
$i++;
}
?>
</optgroup>
</select> OR
<input type="text" id="newcat" name="newcat" /> (Add a new category)</p>
<p><label id="daily">Content</label><br />
<textarea id="daily" name="daily"></textarea></p>
<input type="submit" value="Create Daily" onclick="return checkForm(this.form)" />
</form>
[/html]
First off, don't worry about the PHP. This all works. Also, the
onchange="changeNewCat()" works great too. Let me give you javascript before I go further into this:
// Trying to code up a duplicate stopper!!
var dropdowns = document.getElementsByName("category");
var newCatDrop = document.forms[0].newcat.value;
for(var i = 0; i < dropdowns.value; i++) {
if (i == newCatDrop) {
alert("Sorry, no duplicates allowed.");
return false;
}
else {
// For debugging purposes, will remove later.
alert("No Match");
return false;
}
}The javascript just doesn't work. Theres is more to this. Also, don't worry about the part that says
onclick="return checkForm(this.form)". The above javascript is a snippet, as this all works, except for the javascript stated above.Am I not iterating properly. If someone could guide me or show me a little better way of getting this done, please do show.
I believe this is the last part of the validation of this form and it will be complete.
Any thoughts?
Your iteration is not correct.
Look at your if statement. if (i == newCatDrop).
But what is i? i is an integer variable that is counting the number of times you go through the loop, not the indexes in the drop-down list.
What you want to do is put all of the options into an array. Then, cycle through that array and see if each one is NOT what the user entered. If it is, display an error message. If not, then add it to your list. You're on the right track, you just need to make some minor changes.
Look at your if statement. if (i == newCatDrop).
But what is i? i is an integer variable that is counting the number of times you go through the loop, not the indexes in the drop-down list.
What you want to do is put all of the options into an array. Then, cycle through that array and see if each one is NOT what the user entered. If it is, display an error message. If not, then add it to your list. You're on the right track, you just need to make some minor changes.
•
•
Join Date: Dec 2006
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
Now I have this:
It now pops up with "no match" although, this is a match. So it works, but it doesn't work correctly
.. any help?
// Trying to code up a duplicate stopper!!
var dropdowns = document.getElementsByName("category");
var newCatDrop = document.forms[0].newcat.value;
for(var i = 0; i < dropdowns.length; i++) {
if (dropdowns[i] == newCatDrop) {
alert("Match");
return false;
}
else {
alert("No Match");
return false;
}
}It now pops up with "no match" although, this is a match. So it works, but it doesn't work correctly
.. any help? •
•
Join Date: Dec 2006
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
Could you possibly help out. Telling me and showing me are two different things. I have exhausted all my javascript skills (which isn't much).
Here is my newest attempt:
This almost works, but it doesn't. If you type in 0 (the value of the initial drop down menu option) it works. If you type in "drew" (no quotes) - which is one of the values/options of the drop down menu - does not work.
This is not what I want.
You think someone could manipulate my code to get something working? Let me know. Thanks.
Here is my newest attempt:
var dropdowns = document.forms[0].category;
var newCatDrop = document.forms[0].newcat.value;
for (var i = 0;i < dropdowns.length;i++) {
if (dropdowns.options[i].value == newCatDrop) {
alert("Match");
return false;
}
else {
alert("Not A Match");
return false;
}
}This almost works, but it doesn't. If you type in 0 (the value of the initial drop down menu option) it works. If you type in "drew" (no quotes) - which is one of the values/options of the drop down menu - does not work.
This is not what I want.
You think someone could manipulate my code to get something working? Let me know. Thanks.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- pass the value to the text box (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Vey hard problem.. no idea how to solve ???
- Next Thread: Project help please !!!!!


Linear Mode