JavaScript - Problem With Split Function
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 ); } } Similar TutorialsWhats 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! 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? Hello, I am trying to parse a string using regular expressions. This string can potentially have any number of non-alphanumeric characters acting as delimiters. These delimiters can also be grouped together. Here are some examples of the data: 00923:5342:123 --> I want to extract 00923, 5342 and 123 into an array 08765::764375 --> parse this as 08765 and 764375 into an array 3246//672354//23458 --> parse as 3246, 672354 and 23458 23564-73654 --> etc I'm using the following code: Code: var ids = str.match(/\w*/g); But it just returns the first token from each line and ignores the rest. So with the above data I'm only getting back 00923, 08765, 3246 and 23564 into the first element of the "ids" array. I've also tried an "inverse" approach using the split method like so: Code: var ids = str.split(/\W*/g); and I just get the same result - the first token from each line is only returned. My test code looks like this ('str' contains a string read in from a file): Code: var ids = str.match(/\w*/g); //var ids = str.split(/\W*/g); task.logmsg("the length of the array is " + ids.length); for (i = 0; i < ids.length; i++) { task.logmsg("ids=[" + i + "]=" + ids[i]); } I can confirm that ids.length is returning 1 (??) The 'task' object comes from a separate library and the 'logmsg' method just writes to the console. My reptilian brain is struggling with reg exps. This shouldn't be difficult to do, but I just can't get it to work. If anyone out there can lend some assistance, it would be greatly appreciated. Thanks, JD <p> <script type="text/javascript">// <![CDATA[ var metrics = { "mm" : 1, "cm" : 10, "m" : 1000, "inch" : 25.4, "foot" : 304.8 }; function convert(num, dec){ var val = document.getElementById("fromVal").value; if(isNaN(val)){ return } function roundNumber(num, dec) { var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec); return result; } document.getElementById("toVal").value = val * metrics[document.getElementById("fromSystem").value]/ metrics[document.getElementById("toSystem").value]; } var interval = null; function watchChanges(){ interval == null ? setInterval("convert()", 500) : clearInterval(interval); } // ]]></script> </p> <table> <tbody> <tr> <td><input id="fromVal" style="width: 100px;" onfocus="watchChanges()" onblur="watchChanges()" type="text" /><select id="fromSystem" onchange="convert()"> <option value="mm">millimeters</option> <option selected="selected" value="cm">centimeters</option> <option value="m">meters</option> <option value="foot">feet</option> <option value="inch">inches</option> </select></td> </tr> <tr> <td colspan="1" align="center">=</td> </tr> <tr> <td><input id="toVal" style="width: 100px;" type="text" disabled="disabled" /><select id="toSystem" onchange="convert()"> <option value="mm">millimeters</option> <option value="cm">centimeters</option> <option value="m">meters</option> <option selected="selected" value="foot">feet</option> <option value="inch">inches</option> </select></td> 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? 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> 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/); } 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 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 ... ? 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! 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> I have var att=7,8,9 how can I convert these to an array so I can loop through the values? Code: <select class="drop" name="pet Display" id="drop" onChange="if(this.options[this.selectedIndex].value=='a') {document.getElementById('myFieldSet').style.visibility='visible'} else{document.getElementById('myFieldSet').style.visibility='hidden'}if(this.options[this.selectedIndex].value=='b') {document.getElementById('myFieldSet1').style.visibility='visible'} else{document.getElementById('myFieldSet1').style.visibility='hidden'}if(this.options[this.selectedIndex].value=='c') {document.getElementById('myFieldSet2').style.visibility='visible'} else{document.getElementById('myFieldSet2').style.visibility='hidden'}"> i need to fix this function or a different (external is better )with the same results thanxz Hi, In ASP.NET intranet web page have user input. Have 5 check boxes with two attending text boxes for each. The default value for the text boxes is zero and the text boxes are disabled for input. When the user checks a check box the two corresponding text boxes become enabled for input. Have two command buttons - Submit for database posting and Cancel. I set the Cancel button with a js window.confirm, uncheck the check boxes, and reset any input values in the text boxes back to zero. When I tested the code the checked check boxes were unchecked and the first text box holding user input was reset to zero value. However, the other text boxes containing user input values were not reset to zero. I altered the sequence of the code - for example - list the check box then its text boxes and so on. I tried listing the text boxes first then the check boxes. These did not resolve the problem - I was thinking linear execution of the code. Any suggestions to rememdy this? This is the js function: function Empty() { var res=window.confirm("Please confirm cancellation-your input will be cleared"); if(res==true) { document.getElementById("chkDVTT").value=""; document.getElementById("chkLM").value=""; document.getElementById("chkVMB").value=""; document.getElementById("chkVEB").value=""; document.getElementById("chkVUB").value=""; document.getElementById("txtDVTTRegHrs").value="0"; document.getElementById("txtDVTTOTHrs").value="0"; document.getElementById("txtBRLMRegHrs").value="0"; document.getElementById("txtBrLMOTHrs").value="0"; document.getElementById("txtVMBRegHrs").value="0"; document.getElementById("txtVMBOTHrs").value="0"; document.getElementById("txtVEBRegHrs").value="0"; document.getElementById("txtVEBOTHrs").value="0"; document.getElementById("txtVUBRegHrs").value="0"; document.getElementById("txtVUBOTHrs").value="0"; } } Thanks, John Hello, I'm working on my little grabber and now I want to try something in javascript without success, so here my javascript code: PHP Code: //begin ajax grabber function validateGrabber(b_on_submit){ if(document.grabber.keywords.value == '' || document.grabber.keywords.value == 'search'){ alert('You did not enter a search term. Please try again.'); if(b_on_submit == 'true') return false; } else{ document.grabber.btn.submit(); } } function grabber(inputGrabber) { if(inputGrabber.length == 0) { // Hide the suggestion box. $('#suggestions').hide(); } else if(inputGrabber.length > 2) { $.post(MELODYURL2+'/ajax_grabber.php', {queryString: ""+inputGrabber+""}, function(data){ if(data.length >0) { $('#suggestions').show(); $('#autoSuggestionsList').html(data); } }); } } // end ajax grabber and that's my template code: PHP Code: <form action="{$smarty.const._URL}/search.php" method="get" id="search" name="grabber" onsubmit="return validateGrabber('true');"> <input name="keywords" type="text" value="" class="search_keywords" id="inputGrabber" {if $smarty.const._SEARCHSUGGEST == 1}onkeyup="grabber(this.value);" onblur="fill();" autocomplete="off"{/if} /> <input name="btn" value="{$lang.submit_search}" type="submit" class="search_bttn" /> <div class="suggestionsBox" id="suggestions" style="display: none;"> <div class="suggestionList" id="autoSuggestionsList"> </div> </div> </form> the first function doesn't show the alert and the second doesn't work at all... 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> 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 |