Solved

javascript to check running numbers enter in text field

Posted on 2006-11-29
3
268 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery scrollTop 2 41
Split in Javascript 5 31
Message not shown 5 33
How can I get this SlideToggle to behave? 6 18
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

895 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

14 Experts available now in Live!

Get 1:1 Help Now