for validation - text box and drop down menu

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2006
Posts: 263
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: for validation - text box and drop down menu

 
0
  #11
Dec 16th, 2006
Now here your problem is that as soon as one of those conditions is met, you return something. When you return something, your function is over and it does not continue.

So, here is code that does work. See if you can manipulate it to fit your needs.

  1. <html>
  2. <head>
  3. <script language="javascript" type="text/javascript">
  4. function validate() {
  5. dropdowns = document.forms['theForm'].category;
  6. newCatDrop = document.forms['theForm'].newcat.value;
  7. isThere = false;
  8.  
  9. for (i = 0; i < dropdowns.options.length; i++) {
  10. if (dropdowns.options[i].value == newCatDrop) {
  11. alert(newCatDrop + " is in the list!");
  12. isThere = true;
  13. }
  14. } //close for
  15.  
  16. return isThere;
  17. } //close function
  18. </script>
  19. </head>
  20. <body>
  21. <form name="theForm" action="#" method="post" onsubmit="return validate()">
  22. New category: <input type="text" name="newcat"><br>
  23. Select category: <select name="category">
  24. <option value="0">0</option>
  25. <option value="9">9</option>
  26. <option value="12">12</option>
  27. <option value="23">23</option>
  28. <option value="cats">cats</option>
  29. <option value="dogs">dogs</option>
  30. </select>
  31. <br>
  32. <input type="submit" name="submitButton" value="Submit">
  33. </form>
  34. </body>
  35. </html>
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 10
Reputation: drewrockshard is an unknown quantity at this point 
Solved Threads: 0
drewrockshard drewrockshard is offline Offline
Newbie Poster

Re: for validation - text box and drop down menu

 
0
  #12
Dec 16th, 2006
I appriciate you taking the time to help me out the past few days. The funny thing is, the javascript was sooo close to what you gave me. So, thank you alot. It worked. Here's the new javascript snippet:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var dropdowns = document.forms[0].category;
  2. var newCatDrop = document.forms[0].newcat.value;
  3. for (var i = 0;i < dropdowns.options.length;i++) {
  4. if (dropdowns.options[i].value == newCatDrop) {
  5. alert("Please enter a value that is not already in our database.");
  6. return false;
  7. }
  8. }


Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 10
Reputation: drewrockshard is an unknown quantity at this point 
Solved Threads: 0
drewrockshard drewrockshard is offline Offline
Newbie Poster

Re: for validation - text box and drop down menu

 
0
  #13
Dec 16th, 2006
Oh, and here's another variation:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var dropdowns = document.forms[0].category;
  2. var newCatDrop = document.forms[0].newcat.value;
  3. for (var i = 0;i < dropdowns.options.length;i++) {
  4. if (dropdowns.options[i].value.toLowerCase() == newCatDrop.toLowerCase()) {
  5. alert("Please enter a value that is not already in our database.");
  6. return false;
  7. }
  8. }
I added the toLowerCase() code so that it would dynamically convert the strings to lowercase so that the user can't put in "Drew", "DREW", "drew", or "DrEw", etc. This makes sure that the user can only put one version of "Drew" in. In other words, only one entry (non-case sensitive).

Thought I'd share that bit.
Last edited by drewrockshard; Dec 16th, 2006 at 7:51 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 263
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: for validation - text box and drop down menu

 
0
  #14
Dec 16th, 2006
Glad I could be of help...you were so close which is why I waited to the end to give a possible solution because you were on the right track the whole time and you'll learn it better if you figure it out instead of letting me figure it out for you!

The best thanks is adding to my DaniWeb rep!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC