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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    validate array in javascript

    Hi

    please help me!

    i'm farhad

    i want to validate and replace this array:

    var phl=[
    " Evergreen Valley College;274-7900 ",
    "Dijsktra, Edsger W. ; 510-555-0297",
    "Hoare, C. Anthony R.; 312 555-8763 ",
    "Key Point Software; 249-6625",
    " O'Reilly & Associates; (800)775-7731",
    "Peterson 3rd, Gordon E.; 217-555-1212",
    "Ratchett s P. ; 408 375 - 27651",
    "It's-It Ice Cream Sandwiches;788 -854-5555",
    "The 'Heimlich Maneuver' Café;(111)111-1111",
    "Hapsburg-Hohenzollern, Teresita;888-888-8888",
    "Truman, Harry S;888-888-8888",
    "dair, John 'Red';888-888-8888"];

    to:

    Phone Name
    ----- ----
    (217) 555-1212 Gordon E. Peterson 3rd
    (312) 555-8763 C. Anthony R. Hoare
    (408) 249-6625 Key Point Software
    (408) 274-7900 Evergreen Valley College
    (510) 555-0297 Edsger W. Dijsktra
    (800) 775-7731 O'Reilly & Associates

    TNX

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    this may be a start, but I don't really understand how you expect to get some of those phone number formats - how would the code know, for example, that
    " Evergreen Valley College;274-7900 "
    should become
    (408) 274-7900 Evergreen Valley College

    where does the (408) come from?

    Code:
    for (var i = 0; i < phl.length; i++) {
    var deets=phl[i].split(";");
    var nms=deets[0].split(",");
    var nm=nms.length>1?nms[1]+" "+nms[0]:nms[0];
    document.body.innerHTML+=deets[1]+" "+nm+"<br>";
    }

  • Users who have thanked xelawho for this post:

    saeid1414 (06-29-2013)

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    It would be a much easier function if you just cleared up you ph1 array to begin with.
    Examples:
    1. Leading and trailing spaces for the elements
    2. Consistency in phone number entries,
    ie; number seperator (- or blank) and missing or default area codes, extra long numbers, () around area codes, etc.

    You don't HAVE to do this, but it will make a lot less entry error checking,
    which should have been done in the entry form anyway.
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <meta charset="utf-8">
    
    <script type="text/javascript">
    var phl=[
    " Evergreen Valley College;274-7900 ",
    "Dijsktra, Edsger W. ;510-555-0297",
    "Hoare, C. Anthony R.;312-555-8763 ",
    "Key Point Software;249-6625",
    " O'Reilly & Associates;800-775-7731",
    "Peterson 3rd, Gordon E.;217-555-1212",
    "Ratchett s P. ;408-375-27651",
    "It's-It Ice Cream Sandwiches;788-854-5555",
    "The 'Heimlich Maneuver' Café;111-111-1111",
    "Hapsburg-Hohenzollern, Teresita;888-888-8888",
    "Truman, Harry S;888-888-8888",
    "Adair, John 'Red';888-888-8888"
    ];
    
    function decodePhone(info) {
      var name_phone = info.split(';');
      var tmp = name_phone[1];
      tarr = tmp.split("-");
      if (tarr.length-1 < 2) { tarr.unshift('xxx'); } // xxx is default area code
      tarr[0] = '('+tarr[0]+')';
      return (tarr[0]+' '+tarr[1]+'-'+tarr[2]+' '+name_phone[0]);
    }
    function listPhones() {
      var str = '';
      for (var i=0; i<phl.length; i++) {
        str += decodePhone(phl[i])+'<br>';
      }
      return str;
    }
    </script>
    
    </head>
    <body>
    <button onclick="showPhoneNumbers()">Show Phone List</button>
    <div id="phoneNumbers"></div>
    <script type="text/javascript">
    function showPhoneNumbers() {
      document.getElementById('phoneNumbers').innerHTML = listPhones();
    }
    </script>
    
    </body>
    </html>

  • Users who have thanked jmrker for this post:

    saeid1414 (06-29-2013)

  • #4
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    validetion array in javascript

    One of our university teachers "this Programming Assignment Files" have to practice our
    I do not can to fix it
    Please answer to my send

    we do it using java script

    http://evc-cit.info/cit042/files.html

    Please help
    It is very important

    Thank you


  •  

    Posting Permissions

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