Solved

javascript to check running numbers enter in text field

Posted on 2006-11-29
3
267 Views
Last Modified: 2006-12-29
Hi guys, I have 8 textfields, which I need people to input numbers only 1-8, and each text field value cannot be duplicated.

I need a javascript to check that when I submit the form, any solutions? thanks


0
Comment
Question by:huang_ck
3 Comments
 
LVL 7

Accepted Solution

by:
vbandaru earned 500 total points
Comment Utility
Here you go.. this script is length but it very generic

<html>
<body>
<form>
<input type="text" name="txt1" value=""></br>
<input type="text" name="txt2" value=""></br>
<input type="text" name="txt3" value=""></br>
<input type="text" name="txt4" value=""></br>
<input type="text" name="txt5" value=""></br>
<input type="text" name="txt6" value=""></br>
<input type="text" name="txt7" value=""></br>
<input type="text" name="txt8" value=""></br>
<input type="button" name="btn" value="Validate" onClick="validate()"></br>
</form>
<script language="javascript">
function validate(){
 var arryInput=document.getElementsByTagName("input");
 var arryLength=arryInput.length;
 var aryAllowedVals=new Array("1","2","3","4","5","6","7","8");
 var blnAllowedVal =false;
 var errMsg="";

 for(var i=0;i<arryLength;i++){
   if(arryInput[i].type=="text"){
      for(var k=0;k<aryAllowedVals.length;k++){
            blnAllowedVal=false
             if(arryInput[i].value==aryAllowedVals[k]){
                aryAllowedVals.splice(k,1);
                blnAllowedVal=true;
                break;
              }
      }
      if(!blnAllowedVal){
        errMsg +="A Bad value or duplicate value was entered in :" + arryInput[i].name + "\n";        
      }
   }
  }

if(errMsg!=""){
      errMsg +="The following values were not entered:" + aryAllowedVals.toString()
      alert(errMsg);
      return false;
}
if(aryAllowedVals.length!=0){
  alert("The following values were not entered:" + aryAllowedVals.toString());
  return false;
}
  return true;
}
</script>

</body>
</html>
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now