473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Validating a Select Field

Hello,
I am trying to use javascript to validate a form that has three select inputs. Here is the form:
[HTML]<form name="select" action="main.php" method="post" onsubmit="return ValidateSelect(this)">
<select name="one">
<option value="0" selected="selected">-Select An Option-</option>
</select>
<select name="two">
<option value="0" selected="selected">-Select An Option-</option>
</select>
<select name="three">
<option value="0" selected="selected">-Select An Option-</option>
</select>
<input type="submit" value="Submit" />
<div id="loading" style="display:none;">Loading...</div>
<div id="1Error" style="display:none;">Please Select an Option for 1</div>
<div id="2Error" style="display:none;">Please Select an Option for 2</div>
<div id="3Error" style="display:none;">Please Select an Option for 3</div>
</form>[/HTML]

The Option 0 is not a valid choice and shouldn't be submitted. So I created this javascript to validate it:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function ValidateSelect(form)
  3. {
  4.         var error=false;
  5.     document.getElementById('1Error').style.display = 'none';
  6.     document.getElementById('2Error').style.display = 'none';
  7.     document.getElementById('3Error').style.display = 'none';
  8.  
  9.     document.getElementById('submit').style.display = 'none';
  10.     document.getElementById('loading').style.display = 'block';
  11.     with(form)
  12.     {
  13.         if(one.value=='-1'||one.value=='0')
  14.         {
  15.             document.getElementById('1Error').style.display = 'block';
  16.             error=true;
  17.         }
  18.         if(two.value=='-1'||two.value=='0')
  19.         {
  20.             document.getElementById('2Error').style.display = 'block';
  21.             error=true;
  22.         }
  23.         if(three.value=='-1'||three.value=='0')
  24.         {
  25.             document.getElementById('3Error').style.display = 'block';
  26.             error=true;
  27.         }
  28.         if(error==true)
  29.         {
  30.             document.getElementById('loading').style.display = 'none';
  31.             document.getElementById('submit').style.display = 'block';
  32.             return(false);
  33.         }
  34.         else
  35.         {
  36.             return(true);
  37.         }
  38.     }
  39. }
  40. </script>
For some reason the form submits even if the selects have options selected. I realize that there are no other options for the selects. This is because the options are added by a PHP script that is working perfectly. If my code is confusing (I am almost fluent in JS), here is some pseudo-code:
1. When the form is submitted, hide all error messages and replace the submit button with text that says Loading...
2. Check each select box for an actual selection (Besides the Chose an Option one or not selection at all). If the box doesn't have a selection (or has the Chose an Option one selected), display the error for that box.
3. If there are any missing boxes, replace the loading text with the submit button and cancel the forum's submission. If not, continue with the submission of the form.

If possible, could you please include the full code if you can determine my problem.
Thanks Everyone!!!
Sep 20 '08 #1
2 1713
Code is ok

include name/id property for submit button.

Alternatively use

<input type="button" onClick="ValidateSelect()" value="Submit">

Suggestive script changes:

var form=document.getElementById('select');

In error else block include

form.submit;

This should solve the issue.
Sep 21 '08 #2
Code is ok

include name/id property for submit button.

Alternatively use

<input type="button" onClick="ValidateSelect()" value="Submit">

Suggestive script changes:

var form=document.getElementById('select');

In error else block include

form.submit;

This should solve the issue.
Thanks!!!
That fixed it!!!
Sep 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Samir | last post by:
Say I have 4 forms, all four have different numbers of text boxes. is there a script that I can use to check to make sure everything on the form is not blank?
5
by: palsuddeath | last post by:
Hello all! I'm no programmer, but I have to try to complete some code for a project that we are working on. There will be a web page that is set up to register devices online. Part of the...
8
by: Davey | last post by:
I want to validate that the data entered by a user is correct before they submit the form. How do I validate the following (or what functions/classes do I use): - Ensure a text field is not...
1
by: Brains | last post by:
hello, I am new to access .I am trying to validate a numeric field (Employee number)This field is not auto generated,it is user input field.The data entered in this field should not be less than...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
7
by: Tizzah | last post by:
What is wrong with that? regex = /^(http|https):\/\/+({1}+)*\.{2,5}(({1,5})?\/.*)?$/ if(field.hpage.value != regex.test(field.hpage.value)){ alert("Bad Homepage") field.hpage.focus()...
5
by: Ryan | last post by:
A binding navigator control adds the following code for when the Save button is clicked: Me.Validate() Me.UserBindingSource.EndEdit() Me.UserTableAdapter.Update(Me.UserDataSet.User)" You can...
1
by: NamelessNumberheadMan | last post by:
I can't seem to get Struts 2 validations to work. I have been converting from Strust 1 to Struts 2. So far I've refactored all the code (for this particular module) on the back end, rewrote the jsp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.