JavaScript - Help With Split Csv Into Array
Hi,
I am very new to javascript (been learning over the last 2 days) and I am trying to: 1. create a form that will allow a user to input area codes separated by commas (102,103,209,204,...) 2. remove the commas from the string and store the values in array 3. validate that the values are area code format (ie; greater than 99 but less than 1000) 4. remove any duplicate values before sending to the server I am more or less doing this just to learn (I have ideas for future apps) but know very little about programming. Thank you in advance for any help. The code I have written so far (please excuse the poor programming. I am very new )... Code: <html> <head> <script language="javascript"> function ValidAreaCode(AreaCode) { for(i=0;i<AreaCode.length;i++); var AreaCode = new Array(); AreaCode=input.split(","); function validate() { var n=document.getElementById("myText").value; if(n<99 || n >1000) alert("Please enter a valid area code."); } } //Remove Duplicates var arr = AreaCode(); var unique = arr.unique(); alert(unique); function unique(a) { var r = new Array(); o:for(var i = 0, n = a.length; i < n; i++) { for(var x = 0, y = r.length; x < y; x++) { if(r[x]==a[i]) continue o; } r[r.length] = a[i]; } return r; } </script> </head> <FORM name="MyForm"> Enter area codes with a "," in between them: <input type="text" id="myText"> <input type="submit" value="Submit" onClick="ValidAreaCode()" id="myText"> <P> </form> <br/> </html> Similar TutorialsI have var att=7,8,9 how can I convert these to an array so I can loop through the values? Hi there I'm trying to figure out what the following syntax for split means in the following line of the code arrTest[0].split('/')[0] what does ('/')[0] means in split('/')[0]? please can anyone explain Hi, I'm working on refining my search function. At the moment it works fine like this: Code: function searchLocations() { var found = false; var input = document.getElementById('autocomplete').value; for (var j = 0; j < gmarkers.length; j++) { if (gmarkers[j].myname.toLowerCase() == input.toLowerCase()) { found = true; gmarkers[j].show(); myclick(j); } } if ( ! found ) alert("No matches found. Please check your spelling or refine your search."); } but you have to get the input exactly the same as it is in the array for it to work (as it should, being that that's the way it's written). I wanted to split it so that for example Black Cat Hotel would pop if the user entered Black OR Cat OR Hotel (or any combination of the above). So I thought that this might work: Code: function searchLocations() { var found = false; var input = document.getElementById('autocomplete').value; for (var j = 0; j < gmarkers.length; j++) { var s = gmarkers[j].myname.toLowerCase().split(" "); if ( s == input.toLowerCase()) { found = true; gmarkers[j].show(); myclick(j); } } if ( ! found ) alert("No matches found. Please check your spelling or refine your search."); } but obviously not. Is there some better way to do this? Code: dropDownPart.innerHTML = tmpInnerHTML; how does this tmpInnerHTML gets filled : Code: for (var i = 0; i < arr.length ; i++) { var arr1 = arr[i].split('|'); id_tezaver = arr1[0]; term = arr1[1] // if arr1[1] contains ', then ewerything after that disapears inside term, // like "bird's eyes" becomes "bird" // how to handle that ? // bad example how I get to verify that the stuff is complete inside array // term = arr1[1].replace(/'/i, /oo/); } is there any smart way to do split, and ignore certain delimiters, like: Code: var s = "don't use ',' as part of data, some other text"; arr = s.split(","); // obviously don't want s to be splitted at ',' except chanhing delimiter ... ? Hey I'm trying to create an aggregates calculator where the sum & average are calculated from the user inputs textarea of values, which i have split(). This is my code so far. I'm sure I'm not using the variables or parseFloat method correctly. Any advise? Many Thanks <html> <head> <script type="text/javascript"> <!-- function add() { document.getElementById("answer").value+=(document.getElementById("num1").value) + '\n'; } function calculate() { var total = "sa"; var sa = textAreaText.split("\n"); for (var i=0; i < sa.length; i++) parseFloat(sa[i]) + total; { document.getElementById("sum").value = total; } { document.getElementById("average").value = total / sa.length; } } //--> </script> </head> <body> <h1>Aggregates</h1> <h3>Add as many numbers as you like<br>to the list,then click Calculate.</h3> <form name="entryForm" id="entryForm"> <input type="text" id="num1"></input> <input type="button" value="Add to list" onclick="add();"><br> <textarea name="text" rows="15" cols="20" readonly="readonly" id="answer"></textarea><br> <input type="button" value="Calculate" onclick="calculate"> <input type="reset" value="Reset"> <p>Total (Sum);</p> <input type="text" id="sum" value="0"><br> <p>Average :</p> <input type="text" id="average" value="0"> </form> </body> </html> Hi, I am inserting fields from a .csv file into database using integration engine(rhapsody) there is a javascript filter where I am trying to catch rows that have extra commas in the field text. Tried using the following code but the rows with extra commas just error and don't get inserted. Code: // Loop through all the input messages for (var i = 0; i < input.length; i++) { var next = output.append(input[i]); // Get the body of the current input message var body = input[i].text; // Set the body next.text = body; var name =next.getProperty("BaseFilename"); var fields = name; var fieldsList = fields.split(/\s*,\s*/); if (fieldsList.length >= 10) { name="error"+i; input.setProperty("BaseFilename", name ); } } You may have see various sites where urls are encoded by advertisement redirection service mostly by adf.ly now I want to write a personal java snippet for tor remove this spam [In opera you can specify a custom js to run at every page] mostly the html will look like this Code: <a target="_blank" href="http://adf.ly/246619/http://www.mediafire.com/download.php?lg5z42ra13ac9hi">SteamTable App</a> I want to use .split() method as you see this part http://adf.ly/246619/ [before the second http] has to be splitted some how & url to be reinserted in the a tag. any help here I really don't understand JavaScript... xD Code: var inputDate = "<?php echo $inputDate2; ?>"; var JDates = new Array(); Jdates = inputDate.split("/"); var currentDate = new Date(parseInt(Jdates[2]), parseInt(Jdates[0])-1, parseInt(Jdates[1])); So... when creating 'currentDate' Jdates[0] will equal 0, even though I just asked javascript to split the inputDate by all "/". The inputDate var equals "08/06/2010" before the split. Any help greatly appreciated! Hello I have a javascript navigation bar at the top of my page. I would like to split the navigation with 3 links on the left side of my logo and 3 links on the right side. Like this.... Home Designs Services (LOGO IMAGE) About FAQ's Contact Can someone help me figure out how to split a javascript navigation? I don't know if it should be separate navigation for the right and left or if it is just a CSS change? Below is my javascript and CSS. Code: <script type="text/javascript" src="tinydropdown.js"></script> </script> <div class="nav"> <ul id="menu" class="menu"> <li class="nodiv"><a href="#">Home</a></li> <li><a href="#">Designs</a> <ul> <li><a href="#">One</a></li> <li><a href="#">Two</a></li> <li class="submenu"> <a href="#">Three</a> <ul> <li class="noborder"><a href="#">One</a></li> <li><a href="#">Two</a></li> <li><a href="#">Three</a></li> </ul> </li> </ul> </li> <li><span>Services</span> <ul> <li><a href="#">One</a></li> <li><a href="#">Two</a></li> <li><a href="#">Three</a></li> <li class="submenu"> <span>No Link</span><ul> <li class="noborder"><a href="#">One</a></li> <li><a href="#">Two</a></li> <li><a href="#">Three</a></li> <li><a href="#">Four</a></li> <li><a href="#">Five</a></li> </ul> </li> <li><a href="#">Five</a></li> <li><a href="#">Six</a></li> </ul> </li> <li class="nodiv"><a href="#">About</a></li> <li><a href="#">FAQ's</a> <ul> <li><a href="#">One</a></li> <li><a href="#">Two</a></li> <li class="submenu"> <a href="#">Three</a> <ul> <li class="noborder"><a href="#">One</a></li> <li><a href="#">Two</a></li> <li><a href="#">Three</a></li> </ul> </li> </ul> <li class="nodiv"><a href="#">Contact</a></li></li> </li> </ul> </div> <script type="text/javascript"> var dropdown=new TINY.dropdown.init("dropdown", {id:'menu', active:'menuhover'}); </script> Code: .nav { height:36px; color:#fff; text-shadow:1px 1px #888; position:absolute; z-index: 100; width: 960px; padding-top: 70px; padding-left: 0px; } .menu a { float:left; color:#eee; text-decoration:none; width:120px; height:28px; padding-top:8px; } .menu span { float:left; color:#eee; text-decoration:none; width:120px; height:28px; padding-top:8px; } .menu a:hover { color:#fff; } .menu { list-style:none; font:16px Arial,Verdana; text-align:center; width:900px; margin:0 auto; } .menu li { position:relative; float:left; width:120px; z-index:1000; } .menu ul { display:none; position:absolute; font:normal 13px Arial,Verdana; top:36px; left:0; background:#aaa; display:none; list-style:none; } .menu ul li { float:none; border-top:1px solid #ccc; width:120px; } .menu ul li a, li.menuhover li a, li.menuhover li.menuhover li a { float:none; display:block; background:none; height:22px; padding-top:5px; } .menu ul li a:hover, li.menuhover li a:hover, li.menuhover li.menuhover li a:hover {background:#999; color:#fff; } .menu ul li span, li.menuhover li span, li.menuhover li.menuhover li span { float:none; display:block; background:none; height:22px; padding-top:5px; } .menu ul ul { left:120px; top:0; } .menu li.submenu { font-weight:bold; } .menu li.noborder { border-top:none; } li.menuhover a, li.menuhover li.menuhover a { color:#fff; background:#999; } li.menuhover span, li.menuhover li.menuhover span { color:#fff; background:#999; } THANKS! I have a Text Box which i wish to Place Words into it, once i click the Button it should Split the Words into Characters E.G I enter Before in Text box it should Should B E F O R E Code: <script language="javascript"> function getTag(){ var string = document.getElementById("tag"); var array = string.getElementById("tag"); var finishedArray = []; for (var i = 0, il = array.length; i < il; i++) { var temp = array[i].getElementById(","); for (var j, jl = temp.length; i < jl; j++) { finishedArray.push(temp[j]); destLayer = document.getElementById('splitResponse'); destLayer.innerHTML=''; } } } </script> <h3>Sentence Splitter</h3> <input name="tag" type="text" id="tag" size="30" autocomplete="off"/> <button onClick="getTag();" value="Click"/>Click</button> <div id="splitResponse"></div> <div class="section"></div> </div> Whats wrong with the code below? I'm trying to get the email service provider name. Code: <html> <head> <script type="text/javascript"> function GetProvider { var fullemail = openinviter.email_box.value; var afterat = fullemail.split("@"); var Provider = afterat[1].split("."); var showit = Provider[0]; openinviter.provider_box.value=showit; } </script> </head> <body> <form action="" method="post" name="openinviter"> <input type="text" name="email_box" onchange="GetProvider()" /> <input type="text" name="provider_box" value=""> </form> </body> </html> Thanks alot! Has anyone ever tried to break up a text area into 2 lines, but at the same time each line has a limited number of characters allowed in it ? i.e. line 1 = maxcharacters = 124 , line 2 = machcharacters = 1024 (Line 2 will therefore wrap until 1024 chracters are reached) ... including spaces as characters. Something along the lines of: Code: var maxLines = txtArea.rows; var maxChars = txtArea.rows * txtArea.cols; countChars.value = txtArea.value.length; countLines.value = getLines(txtArea); document.myForm.maxLines.value = maxLines; document.myForm.maxChars.value = maxChars; if((txtArea.value.length >= maxChars || getLines(txtArea) >= maxLines) && (window.event.keyCode == 10 || window.event.keyCode == 13)) { while(getLines(txtArea) > maxLines) txtArea.value = txtArea.value.substr(0,txtArea.value.length-2); while(txtArea.value.length > maxChars) txtArea.value = txtArea.value.substr(0,txtArea.value.length-2); alert("chars and / or lines limit reached"); } else if(txtArea.value.length > maxChars ) { while(txtArea.value.length > maxChars) { txtArea.value = txtArea.value.substr(0,txtArea.value.length-1); } alert("chars limit reached"); } else if (f (countLines.value > maxLines) { while(countLines.value > maxLines) { txtArea.value = txtArea.value.substr(0,txtArea.value.length-1); } alert("lines limit reached"); } countChars.value = txtArea.value.length; countLines.value = getLines(txtArea); } Thanks Is it possible to SplitText this way? The SplitText function has a unique name for each task Split1 - Split1(this.value) Split2 - Split2(this.value) Split3 - Split3(this.value) Code: <script type="text/javascript"> function Split1 (info) { if (info == '') { return; } var tarr = info.split(","); document.getElementById('field1').value = tarr[0]; document.getElementById('field2').value = tarr[1]; } function Split2 (info) { if (info == '') { return; } var tarr = info.split(","); document.getElementById('field3').value = tarr[2]; document.getElementById('field4').value = tarr[3]; } function Split3 (info) { if (info == '') { return; } var tarr = info.split(","); document.getElementById('field5').value = tarr[4]; document.getElementById('field6').value = tarr[5]; } </script> I have a web form that requests a user full name and email address. What I would like to do is when the user fills in the full name in one input box, I would like to take the full name and populate the email address field as such. "first.last@allstate.com". So pretty much split the full name and popluate email address field "first.last@allstate.com". Tracy i see alot of forms where the phone number field is split into 3 boxes and you can type smoothly from the first box to the last....here is the beginning code i have now for my form Code: <form id="contactform" action="contact-submit.php" method="post"> <!-- form fields --> <div class="form"> <label for="name">Name<em>•</em></label> <input class="textbox required" type="text" name="name" id="name" /><br /> <label for="email">E-Mail<em>•</em></label> <input class="textbox required email" type="text" name="email" id="email" /><br /> how do i add a phone number field in there that has 3 connected fields? <!-- ** Need help completing the javascript in this example ** --> Code: <html> <head> <title>Convert ListBox data into 4 Text Boxes</title> <script language="javascript"> function SplitText ( // NEED HELP WITH THIS CODE split text ","; convert to ['textbox1','textbox2','textbox3','textbox4'] } </script> </head> <body> <form name="convert"> <p style="margin: 2px"> <h3>Convert selected data to textboxes</h3> <!-- SELECT DATA FROM LIST BOX --> <select size="1" name="ListBox" onChange="SplitText();"> <option value="jim,bob,rick,paul" >==Row1==</option> <option value="pete,jack,chris,craig">==Row2==</option> <option value="mary,jane,lisa,kim" >==Row3==</option> </select> </p> <h3>In this example say we selected row1</h3> <!-- THE SELECTED DATA GETS SPLIT AND INSERTED INTO THESE 4 TEXTBOXES --> <p style="margin: 2px"> <input type="text" name="textbox1" value="jim"><br> <input type="text" name="textbox2" value="bob"><br> <input type="text" name="textbox3" value="rick"><br> <input type="text" name="textbox4" value="paul"><br> </p> </form> </body> </html> Hi there! I'm trying to retrieve the integer value in the string: #10 So I'm using the JS Split function Code: <script type="text/javascript"> var str="#10"; document.write(str.split("#")); </script> Is the code I'm using, but I keep getting a comma before my result.. in this case, my result is ",10" What should I do? Thanks! Kind Regards Matthew P.S. I know that it's an array, but is there still anyway for me to get rid of the comma? I have searched wide and far for something like this. Thought I would find it because it is something that has become quite big these days - split testing. Possibly some php to put in the head that would open and get a value from a text file. myfile.txt Inside the myfile.txt file is either a 1 or a 0. Each time it is opened to read/write, it switches... 1 for 0, or 0 for 1. A switch or toggle. Then in the body, javascript only (cannot use php in the body because I want to be able to add javascript into a wordpress or similar blog post) and a value could be sent from the php in the head (hardcoded) to the javascript... IF the var is 1, then "http://linkA.php" ELSE "http://linkB.php" The php can read (get the number in the myfile.txt), then write with the other - 1 or 0. No matter what page I add the js script to, each post can use the php code values in the <head> / the myfile.txt file. This seems like it should be straight forward, and I know it can get a little more involved if we first check for a file, and if no exist, then create... Thank you, Steve |