Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript to check running numbers enter in text field

Posted on 2006-11-29
3
Medium Priority
?
275 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

886 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