473,382 Members | 1,420 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,382 software developers and data experts.

Unstoppable Form Validation

Hi..guys! I have a function here to validate null selection of drop down menu and radio button.However,it seems like alert box couldn't be stopped display even though both fileds were already selected.Thanks for your generious help

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/JavaScript">
  2. function nullValidation() {
  3.     var x=document.getElementById("movieList");
  4.     for (var i=0;i<x.length;i++)
  5.     {
  6.       if(x.elements[i].checked=="" || x.elements[i].selectedIndex == 0) 
  7.       {
  8.         alert("Please select "+x.elements[i].title+".");
  9.         x.elements[i].focus();
  10.         return false;
  11.       }
  12.  
  13.     }
  14. return true;
  15. }
  16. </script>
  17.  
Expand|Select|Wrap|Line Numbers
  1. <form action="" method="post" name="movieList" 
  2. onSubmit="return nullValidation();">
  3.  
Sep 24 '07 #1
3 1089
mrhoo
428 256MB
for (var i=0;i<x.length;i++) {
if(x.elements[i].checked=="" || x.elements[i].selectedIndex == 0) {
Chances are you have an element that does not have both a checked and a selectedIndex attribute.
Sep 24 '07 #2
r035198x
13,262 8TB
Hi..guys! I have a function here to validate null selection of drop down menu and radio button.However,it seems like alert box couldn't be stopped display even though both fileds were already selected.Thanks for your generious help

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/JavaScript">
  2. function nullValidation() {
  3.     var x=document.getElementById("movieList");
  4.     for (var i=0;i<x.length;i++)
  5.     {
  6.       if(x.elements[i].checked=="" || x.elements[i].selectedIndex == 0) 
  7.       {
  8.         alert("Please select "+x.elements[i].title+".");
  9.         x.elements[i].focus();
  10.         return false;
  11.       }
  12.  
  13.     }
  14. return true;
  15. }
  16. </script>
  17.  
Expand|Select|Wrap|Line Numbers
  1. <form action="" method="post" name="movieList" 
  2. onSubmit="return nullValidation();">
  3.  
Shouldn't elements be options instead?
Sep 24 '07 #3
Thanks for reply.Problem resolved.

Expand|Select|Wrap|Line Numbers
  1. <form action="" method="post" name="movieList" 
  2.           onSubmit="return validate(this);">
  3.  
  4. <script language="JavaScript" type="text/JavaScript">
  5.     function validate(which) {
  6.         var selects = which.getElementsByTagName('select');
  7.         var radios = which.getElementsByTagName('input');
  8.         for(sel = 0; sel < selects.length; sel++) {
  9.             if(selects[sel].options[selects[sel].selectedIndex].text == 'Select') {
  10.                 alert('error [select]');
  11.                 return false;
  12.             }
  13.         }
  14.  
  15.         var rselCount = 0;
  16.         for(radio = 0; radio < radios.length; radio++) {
  17.             if(radios[radio].checked) {
  18.               rselCount++;
  19.             }
  20.         }
  21.  
  22.         if(rselCount == 0) {
  23.            alert('error [radio]');
  24.            return false;
  25.         }
  26.         return true;
  27.     }
  28.  
  29. </script>
  30.  
  31.  
Sep 24 '07 #4

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

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
4
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
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...
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...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
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...
4
by: mkashif1 | last post by:
Hi, I'm developing a service that will be monitoring some of the activities of currently logged in user. one of the requirement is that the service must be unstoppable. The user (any user even...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.