Solved

how to check values entered are  in greater order...using javascript..

Posted on 2009-07-08
6
596 Views
Last Modified: 2012-08-14

        The addcandidate function ,will add two textboxes of count 10 , I need to validate , that
entered values of textboxes follows  this...
1.Candidate --> Entered value should be greater always to the previous candidate texbox
1.Percent------->Entered values can be greater or equal to the previous percent textbox ...

how can i validate this peculiar condition plzz help??

  i.e (entered textbox value should be like this)
  candidate percent
   15              7
   16              9
   20             10
   22             12
   25             12
   30             20

if any entered values goes wrong should alert....
<a href="javascript:;" onclick="addcandidate()">Add candidatete</a>
    <input type="text" name="candidate1"   id="candidate1" runat="server"  />
    <input type="text" name="percent1" id="percent1" runat="server"  />
    <input type="text" name="candidate2" id="candidate2"  runat="server"  />
    <input type="text" name="percent2"  id="percent2" runat="server"  />
    ;;;;;;;;;;;;;;;;;
   <input type="text" name="candidate10" id="candidate10"  runat="server"  />
   <input type="text" name="percent10"  id="percent10" runat="server"  />
      <asp:Button id="Button1" runat="server" OnclientClick="return validate();"/>

Open in new window

0
Comment
Question by:Rajar Ahmed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 6

Assisted Solution

by:coolersport
coolersport earned 500 total points
ID: 24810385

<script type="text/javascript">
<!--
function validate() {
  var lastCandidate = 0; // minimum starting candidate
  var lastPercent = 0; // minimum starting percent
  var e;
  for (var i = 1; i <= 10; i++) {
    e = parseInt(document.getElementById('candidate' + i).value);
    if (e > lastCandidate) { // notice the greater than sign
      lastCandidate = e;
    } else {
      alert('Candidate must be greater than the previous'); // you may show error here
      return false;
    }
    e = parseInt(document.getElementById('percent' + i).value);
    if (e >= lastPercent) { // notice the greater than or equal sign
      lastPercent = e;
    } else {
      alert('Percent must be greater than or equal the previous'); // you may show error here
      return false;
    }
  }
  return true;
}
-->
</script>

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24810476
validation part work wells,
 but if the user created only till  candidate4 textbox(for eg)  using addcandidate function()
 am getting OBJECT expected error
 Eg:in this case user want to create till candidate4..
 23 6  (candidate1 percent1)
 25 9  (candidate2 percent2)
 30 9  (candidate3 percent3)
 40 10 (candidate4 percent4)
(need not to be create after that ..at this time i get objectexpected error..)
 
 It is not necc that ,that all ten candidate value should be entered .
 If 10 candidate textbox is created not getting error, but if less than that am getting error...
 plz help....
0
 
LVL 2

Expert Comment

by:jainnaveen
ID: 24810522
i see CoolerSport's answer goes all well.

And follow below for his code should work fro you.

name all canditate textbox as canditates and get lenth of textboxes using
var len = document.all.item("canditates ").length; and use in place of 10 in line -- for (var i = 1; i <= len; i++)


And loop  accordingly.

cheers,
Naveen
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:coolersport
ID: 24810525

<script type="text/javascript">
<!--
function validate() {
  var lastCandidate = 0; // minimum starting candidate
  var lastPercent = 0; // minimum starting percent
  var e;
  var input;
  for (var i = 1; i <= 100; i++) {
    input = document.getElementById('candidate' + i);
    if (!input) break;
    e = parseInt(input.value);
    if (e > lastCandidate) { // notice the greater than sign
      lastCandidate = e;
    } else {
      alert('Candidate must be greater than the previous'); // you may show error here
      return false;
    }
  }
  for (var i = 1; i <= 100; i++) {
    input = document.getElementById('percent' + i);
    if (!input) break;
    e = parseInt(input.value);
    if (e >= lastPercent) { // notice the greater than or equal sign
      lastPercent = e;
    } else {
      alert('Percent must be greater than or equal the previous'); // you may show error here
      return false;
    }
  }
  return true;
}

Open in new window

0
 
LVL 6

Accepted Solution

by:
coolersport earned 500 total points
ID: 24810533
Last one lacks closing tag, so it doesn't render properly in EE :D

<script type="text/javascript">
<!--
function validate() {
  var lastCandidate = 0; // minimum starting candidate
  var lastPercent = 0; // minimum starting percent
  var e;
  var input;
  for (var i = 1; i <= 100; i++) {
    input = document.getElementById('candidate' + i);
    if (!input) break;
    e = parseInt(input.value);
    if (e > lastCandidate) { // notice the greater than sign
      lastCandidate = e;
    } else {
      alert('Candidate must be greater than the previous'); // you may show error here
      return false;
    }
  }
  for (var i = 1; i <= 100; i++) {
    input = document.getElementById('percent' + i);
    if (!input) break;
    e = parseInt(input.value);
    if (e >= lastPercent) { // notice the greater than or equal sign
      lastPercent = e;
    } else {
      alert('Percent must be greater than or equal the previous'); // you may show error here
      return false;
    }
  }
  return true;
}
-->
</script>

Open in new window

0
 
LVL 18

Author Closing Comment

by:Rajar Ahmed
ID: 31601410
Great...Made it so simple...it also Solved my various complexity of validations used before...Thansk :)...!!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Print Rhino Java Array in Javascript 1 39
VB.Net  Exporting Excel Woorkbook to PDF 7 60
Add - in a loop to first and last element. 6 42
VB.net VSTO Excel Ribbon error 4 14
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question