www.webdeveloper.com
Results 1 to 14 of 14

Thread: I need a second set of eyes

  1. #1
    Join Date
    Mar 2012
    Posts
    41

    I need a second set of eyes

    Im trying to add some serial numbers from a number n of text boxes to an array at the same time I want to make sure that no duplicates are being entered. Here is my code:

    Code:
    function GetSerials() {
    	
    	var count_1 = 0;
    	var serialArray = new Array;
    	var id = "1serial_"+count_1;
    	var element = document.getElementById(id).value;
    	serialArray.length = 1;
    	try
    	{	
    		duplicate:	
    		while (element != null)
    		{
    			var id = "1serial_"+count_1;
    			var element = document.getElementById(id).value;			
    			for(x=0;x<serialArray.length;x++)
    			{
    				if(element == serialArray[x])
    				{
    					alert("You can not use duplicate serial numbers");
    					serialArray.length = 0;
    					break duplicate;
    				}
    				else
    				{
    					serialArray.push(element);
    				}				
    			}
    			count_1++;			
    		}
    	}catch(err)
    	{
    		alert(serialArray);
    	}	
    }
    Thanks for your help.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,031
    this seems to work ok:
    Code:
    <html>
    <head>
    </head>
    <body>
    <input type="text"/>
    <input type="text"/>
    <input type="text"/>
    <input type="text"/>
    <input type="text"/>
    <input type="button" onclick="checkDupes()" value="check"/>
    <script>
    var tbs=document.getElementsByTagName("input");
    var serials=[];
    function checkDupes(){
    serials.length=0;
    for (var i = 0; i < tbs.length; i++) {
    if (tbs[i].type=="text"){
    if (serials.indexOf(tbs[i].value)==-1){
    serials.push(tbs[i].value);
    		} else {
    	alert("You can not use duplicate serial numbers - serial number "+tbs[i].value+ " repeated");
    	return;
    			}
    		}
    	}
    alert(serials.toString())	
    }
    </script>
    </body>
    </html>

  3. #3
    Join Date
    Aug 2007
    Posts
    3,767
    It might be easier to create an object of serial numbers, and then put them into the array, depending on how many you have.
    Code:
    var dupes = {};
    var serialNumbers = [];
    for (var i = 0; i < how_ever_many; i++) {
      var e = /* however you get them */;
      if (dupes[e]) {
        // Duplicate
      }
      else {
        dupes[e] = true;
        // not duplicate
      }
    }
    Obviously you can't just rely on JavaScript to do this if you're uploading it to a server or something, this is just an initial check.
    Great wit and madness are near allied, and fine a line their bounds divide.

  4. #4
    Join Date
    Mar 2012
    Posts
    41
    Im going to try both method and yes Im using AJAX/php to put things on the server the problem Im having is that this serial numbers may be accompannied by a small comment and on top of that this are pipes used in the oil business, therefore they have also a size. My boss kinda threw me under the bus with this one.

  5. #5
    Join Date
    Mar 2012
    Posts
    202
    the array.indexOf(variable) method i'm pretty sure works to search arrays, correct me if i'm wrong

  6. #6
    Join Date
    Mar 2012
    Posts
    202
    if you are searching for a string inside of a string you may also want to try regular expressions here is a good resource for that http://www.w3schools.com/js/js_obj_regexp.asp

  7. #7
    Join Date
    Mar 2012
    Posts
    202
    Sorry I'm posting again that URL isn't the one I exactly meant to give and I can't find the edit post button. Here is the correct URL http://validator.w3schools.com/jsref...obj_regexp.asp

  8. #8
    Join Date
    Mar 2012
    Posts
    41
    Not realy looking for a string within a string just grabbing some values from a form for an inventory type of system. Here is the dev page: http://larsoncomputerservices.com/de...separator.html

  9. #9
    Join Date
    Mar 2012
    Posts
    202
    seems to be working fine

  10. #10
    Join Date
    Mar 2012
    Posts
    41
    yea so far the only thing that I cant seem to figure out is how to tie a comment to a particular serial number to then sent it to be saved in the database

  11. #11
    Join Date
    Jan 2011
    Posts
    117
    you should use an Object instead
    Code:
    var serials = {};
    serials[curentSerial] = curentSerial;
    //or bettermore:
    Code:
    serials[curentSerial] = index;
    this way dpes will be overwritten automatically. And you can access them wit the in operator +check the index value so you would also know which field was dupped.

  12. #12
    Join Date
    Mar 2012
    Posts
    202
    If you absolutely need to tie the variable together with a comment then first encode the string somehow, like by using the escape() function then add it to the serial using a separator character such as a : or | then when you need to search for duplicate serials use a RegExp object, i'm pretty sure it can be used with indexOf.

  13. #13
    Join Date
    Aug 2007
    Posts
    3,767
    The best way is the object. The structure you'll be looking for eventually is:
    Code:
    [{"id":011010101001,"comment":"Whatever"},{...},{...}];
    Or something along those lines. You could obviously just use an array for each id,comment pair too.
    Great wit and madness are near allied, and fine a line their bounds divide.

  14. #14
    Join Date
    Mar 2012
    Posts
    202
    Quote Originally Posted by Declan1991 View Post
    The best way is the object.
    True, but I think he wants to find a way to store it in a database, not just a variable

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles