Reply
form validation - text box and drop down menu
Old 12-15-2006, 11:16 PM form validation - text box and drop down menu
drewrockshard's Avatar
Super Talker

Posts: 100
Name: Drew
Location: Garland (Dallas), Texas
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 exists in the drop down menu, I want a pop up to say "Category already exists" 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.

Here's what i have so far:

HTML Code:
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>
First off, don't worry about the PHP. This all works. Also, the onchange="changeNewCat()" works great too.

Code:
        // 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;
                }
        }
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?
__________________
Best Regards,
Drew Decker
http://www.deckerd.com
drewrockshard is offline
Reply With Quote
View Public Profile Visit drewrockshard's homepage!
 
When You Register, These Ads Go Away!
Old 12-16-2006, 05:27 AM Re: form validation - text box and drop down menu
drewrockshard's Avatar
Super Talker

Posts: 100
Name: Drew
Location: Garland (Dallas), Texas
Sorted.

Fix? Thanks to another guy at daniweb.com.

Here's the javascript correction:

Code:
        var dropdowns = document.forms[0].category;
        var newCatDrop = document.forms[0].newcat.value;
        for (var i = 0;i < dropdowns.options.length;i++) {
                if (dropdowns.options[i].value == newCatDrop) {
                        alert("Please enter a value that is not already in our database.");
                        return false;
                }
        }
__________________
Best Regards,
Drew Decker
http://www.deckerd.com
drewrockshard is offline
Reply With Quote
View Public Profile Visit drewrockshard's homepage!
 
Reply     « Reply to form validation - text box and drop down menu
 

Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11353 seconds with 12 queries