Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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: &nbsp;&nbsp;&nbsp;<input type="text" id="myText">
    <input type="submit" value="Submit" onClick="ValidAreaCode()" id="myText">
    
    <P>
    
    </form>
    
    
    <br/>
    
    
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by jsnewb View Post
    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
    This is not a very good way to go about it. What happens if an invalid code is entered? It that code ignored? Or does the user have to retype the whole line of codes?


    Try this:-

    Code:
    <script type = "text/javascript">
    
    function aCodes() {
    var codes = [];
    var ans;
    while (ans = window.prompt("Enter the next area code, then OK, or Cancel to end"," ")) {
    ans = ans.replace(/^\s+/,"");  // strip leading spaces
    if ((!/^\d{3}$/gi.test(ans)) || (ans == "")) {  // must be 3 digits
    alert ("You must enter a valid 3-digit area code!!  ");
    }
    else {
    codes.push(ans);
    }
    }
    //Remove Duplicates
    codes.sort();
    for (var i = 1; i < codes.length; i++) {
    if (codes[i-1] == codes[i])
    codes.splice(i, 1);
    }
    alert (codes);
    }
    
    aCodes()
    
    </script>

    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

  • #3
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply and help. Your code works great! But I was hoping to post an error for an invalid entry after evaluating the string, and yes, the user would have to retype their entry.

    If I got that part working, I had seen some code that would highlight the invalid portion of their entry. I am not sure if that would work for this, but I was hoping to get the split portion of the code working (mostly because I have a vendetta now...)

    Thanks for the help.


  •  

    Tags for this Thread

    Posting Permissions

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