Solved

javascript to check running numbers enter in text field

Posted on 2006-11-29
3
272 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
[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 Comments
 
LVL 7

Accepted Solution

by:
vbandaru earned 500 total points
ID: 18038951
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

751 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