473,401 Members | 2,146 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,401 software developers and data experts.

Enforce a Specific Field Form Validation with Javascript

Hi.

Using javascript, how can I enforce users to have to select a value from a dropdown list before proceeding to entering other values on a form?

Steph
Aug 9 '10 #1

✓ answered by johny10151981

in your code
Expand|Select|Wrap|Line Numbers
  1. <select name="test" id="test">
  2. <option value="1">1</option>
  3. <option value="2">2</option>
  4. <option value="3">3</option>
  5. </select>
  6.  
The default selected is 1
you better add another option
Expand|Select|Wrap|Line Numbers
  1. <option value="0" SELECTED></option>
now add an onChange method to your select menu. Check the condition. if it fulfills the condition then enable the Submit button.

All can be done by javascript

6 2016
johny10151981
1,059 1GB
Disable all the fields,
When user would choose an item from drop down list enable them all.
Aug 9 '10 #2
Thanks for your quick reply.

Expand|Select|Wrap|Line Numbers
  1. function disableAll(){
  2. var elem = document.forms[0].elements;
  3. for(var i=0;i<elem.length;i++){
  4. elem[i].setAttribute('disabled',true)
  5. }
  6. }
  7.  
I think I got the code right, but I need some guidance to implement it correctly. Here is a quick form:

Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="form1" method="post" action="form.asp">
  2.   <p>
  3.     <label>Name
  4.       <input type="text" name="Name" id="Name" />
  5.     </label>
  6.   </p>
  7.   <p>
  8.     <label>List
  9.       <select name="test" id="test">
  10.         <option value="1">1</option>
  11.         <option value="2">2</option>
  12.         <option value="3">3</option>
  13.       </select>
  14.     </label>
  15.   </p>
  16.   <p>
  17.     <label>
  18.       <input type="radio" name="radio" id="button" value="button" />
  19.       button</label>
  20.   </p>
  21.   <p>&nbsp;</p>
  22. </form>
  23.  
I don't want the user to click on the button before he select's a value from the above dropdown list.

Thanks,
Steph
Aug 9 '10 #3
johny10151981
1,059 1GB
in your code
Expand|Select|Wrap|Line Numbers
  1. <select name="test" id="test">
  2. <option value="1">1</option>
  3. <option value="2">2</option>
  4. <option value="3">3</option>
  5. </select>
  6.  
The default selected is 1
you better add another option
Expand|Select|Wrap|Line Numbers
  1. <option value="0" SELECTED></option>
now add an onChange method to your select menu. Check the condition. if it fulfills the condition then enable the Submit button.

All can be done by javascript
Aug 9 '10 #4
Sorry i ve to use this means but its related to my problem

my form is submited even if my onsubmit function returns false

for example
Expand|Select|Wrap|Line Numbers
  1. function validdata(){
  2.  alert('no success');
  3.  return false;
  4. }
  5.  
  6. <form method='post' action='somefile.php' onsubmit='validdata();'>
  7. <input type='text />
  8. <input type='submit'/>
  9. </form>
  10.  
  11.  
i see the alert but the form still get submited
Aug 9 '10 #5
Sorry i ve to use this means but its related to my problem

my form is submited even if my onsubmit function returns false

for example
Expand|Select|Wrap|Line Numbers
  1. function validdata(){
  2.  alert('no success');
  3.  return false;
  4. }
  5.  
  6. <form method='post' action='somefile.php' onsubmit='validdata();'>
  7. <input type='text />
  8. <input type='submit'/>
  9. </form>
  10.  
  11.  
i see the alert but the form still get submited
Aug 10 '10 #6
johny10151981
1,059 1GB
check line 6.
try
onsubmit="return validdata();"

It will work
Aug 10 '10 #7

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

Similar topics

2
by: megabyte | last post by:
Hello; I have an ASP application which gathers information to put in a database via some forms - I would like to do the form validation (making sure some fields are not empty) with a Javascript...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
1
by: iMedia User | last post by:
I have a site where I want to use the Web form validators in two separate forms on a single page. One form allows existing users to log in while the second one allows new users to register. The...
4
by: bienwell | last post by:
Hi, I have a problem and really need your help. In my web page ASPX, I have some text fields to accept data from users. I did form validation like this : <td class="dataTD" style="HEIGHT:...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
5
by: mattmao | last post by:
Greetings everyone. The Newbie is coming back from uni... I knew making a good form validation JavaScript would be a piece of cake for you guys, but as a novice programmer as me, I still find...
9
hgeithus
by: hgeithus | last post by:
Hi. I'm doing a form at my website, and I found this document which is very interesting: Javascript form validation - doing it right . I have a little question though. All the documents within my...
7
by: kkshansid | last post by:
i want to know what is most trusted way for form validation javascript or php?? i also want to know what technology is used by naukri.com
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.