Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Well, soneone's gotta go first...

Well, soneone's gotta go first...

Scheduled Pinned Locked Moved JavaScript
javascriptdata-structuresregexhelp
6 Posts 3 Posters 26 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NeverHeardOfMe
    wrote on last edited by
    #1

    ..may as well be me! This is perhaps a little cheeky to ask, but hey... my JavaScript skills are functional (shall we say) but I've never really mastered it... in particular, the use of arrays.... I have a FileUpload control, fu2, with the following attribute added in code behind:

    fu2.Attributes.Add("onchange", "checkFile(this)")

    which call this verification function in my JavaScript file:

    function checkFile(what){
    var source=what.value;
    if (source!='') {
    var i = source.lastIndexOf("\\");
    var j = source.lastIndexOf(".");
    var fName = source.substring(i+1,j);
    var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
    if ( ext=='doc' || ext=='docx' || ext=='pdf' || ) {
    var regex = /^[A-Za-z0-9_ -]{1,50}$/;
    if (!regex.test(fName)) {
    alert('The file name contains illegal characters, or is too long\nPlease re-name the file using only alphanumeric charcters, hyphens, spaces and underscores\nand restrict it to a maximum of 50 characters\n' + fName);
    }
    } else {
    alert('Please upload only files of the following types:\n .doc, .docx, .pdf');
    }
    }
    }

    As you can see, this attempts to limit both the file types the user can upload, and also the file name. And actually, it all works well enough - but I know it could be written so much better... I feel that the "list" of allowable file types should be an array, and the test made against that, as well as the alert message built out of it - thus making the code that much easier to maintain if the list changes. Arrays.. hmm.. this is where I get stuck... plz, it's not urgentz, but if anyone is feeling generously inclined.... :-)

    D M 2 Replies Last reply
    0
    • N NeverHeardOfMe

      ..may as well be me! This is perhaps a little cheeky to ask, but hey... my JavaScript skills are functional (shall we say) but I've never really mastered it... in particular, the use of arrays.... I have a FileUpload control, fu2, with the following attribute added in code behind:

      fu2.Attributes.Add("onchange", "checkFile(this)")

      which call this verification function in my JavaScript file:

      function checkFile(what){
      var source=what.value;
      if (source!='') {
      var i = source.lastIndexOf("\\");
      var j = source.lastIndexOf(".");
      var fName = source.substring(i+1,j);
      var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
      if ( ext=='doc' || ext=='docx' || ext=='pdf' || ) {
      var regex = /^[A-Za-z0-9_ -]{1,50}$/;
      if (!regex.test(fName)) {
      alert('The file name contains illegal characters, or is too long\nPlease re-name the file using only alphanumeric charcters, hyphens, spaces and underscores\nand restrict it to a maximum of 50 characters\n' + fName);
      }
      } else {
      alert('Please upload only files of the following types:\n .doc, .docx, .pdf');
      }
      }
      }

      As you can see, this attempts to limit both the file types the user can upload, and also the file name. And actually, it all works well enough - but I know it could be written so much better... I feel that the "list" of allowable file types should be an array, and the test made against that, as well as the alert message built out of it - thus making the code that much easier to maintain if the list changes. Arrays.. hmm.. this is where I get stuck... plz, it's not urgentz, but if anyone is feeling generously inclined.... :-)

      D Offline
      D Offline
      daveyerwin
      wrote on last edited by
      #2

      NeverHeardOfMe wrote:

      I feel that the "list" of allowable file types should be an array, and the test made against that

      <html><head><script>
      var fileTypeAllowed = (function(){
      var ar = arguments;
      return function(arg){
      for(var i = ar.length;i--;){
      if(arg == ar[i]){return true;}}
      return false;
      };})('doc','docx','pdf');
      alert(fileTypeAllowed('docx'));
      alert(fileTypeAllowed('docxa'));
      </script>
      </head>
      <body>
      hi
      </body></html>

      modified on Tuesday, September 14, 2010 12:44 PM

      N 1 Reply Last reply
      0
      • D daveyerwin

        NeverHeardOfMe wrote:

        I feel that the "list" of allowable file types should be an array, and the test made against that

        <html><head><script>
        var fileTypeAllowed = (function(){
        var ar = arguments;
        return function(arg){
        for(var i = ar.length;i--;){
        if(arg == ar[i]){return true;}}
        return false;
        };})('doc','docx','pdf');
        alert(fileTypeAllowed('docx'));
        alert(fileTypeAllowed('docxa'));
        </script>
        </head>
        <body>
        hi
        </body></html>

        modified on Tuesday, September 14, 2010 12:44 PM

        N Offline
        N Offline
        NeverHeardOfMe
        wrote on last edited by
        #3

        See... I knew it would be obvious.... :sigh: Thanks :thumbsup: (Sorry for not marking your answer as "Good" - done now!)

        modified on Friday, July 23, 2010 4:19 AM

        D 1 Reply Last reply
        0
        • N NeverHeardOfMe

          See... I knew it would be obvious.... :sigh: Thanks :thumbsup: (Sorry for not marking your answer as "Good" - done now!)

          modified on Friday, July 23, 2010 4:19 AM

          D Offline
          D Offline
          daveyerwin
          wrote on last edited by
          #4

          NeverHeardOfMe wrote:

          See... I knew it would be obvious.... Thanks

          There is nothing obvious about that code :-D

          1 Reply Last reply
          0
          • N NeverHeardOfMe

            ..may as well be me! This is perhaps a little cheeky to ask, but hey... my JavaScript skills are functional (shall we say) but I've never really mastered it... in particular, the use of arrays.... I have a FileUpload control, fu2, with the following attribute added in code behind:

            fu2.Attributes.Add("onchange", "checkFile(this)")

            which call this verification function in my JavaScript file:

            function checkFile(what){
            var source=what.value;
            if (source!='') {
            var i = source.lastIndexOf("\\");
            var j = source.lastIndexOf(".");
            var fName = source.substring(i+1,j);
            var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
            if ( ext=='doc' || ext=='docx' || ext=='pdf' || ) {
            var regex = /^[A-Za-z0-9_ -]{1,50}$/;
            if (!regex.test(fName)) {
            alert('The file name contains illegal characters, or is too long\nPlease re-name the file using only alphanumeric charcters, hyphens, spaces and underscores\nand restrict it to a maximum of 50 characters\n' + fName);
            }
            } else {
            alert('Please upload only files of the following types:\n .doc, .docx, .pdf');
            }
            }
            }

            As you can see, this attempts to limit both the file types the user can upload, and also the file name. And actually, it all works well enough - but I know it could be written so much better... I feel that the "list" of allowable file types should be an array, and the test made against that, as well as the alert message built out of it - thus making the code that much easier to maintain if the list changes. Arrays.. hmm.. this is where I get stuck... plz, it's not urgentz, but if anyone is feeling generously inclined.... :-)

            M Offline
            M Offline
            mmagill0
            wrote on last edited by
            #5

            Just to let you know, you spelt characters wrong in the alert() function :)

            NeverHeardOfMe wrote:

            only alphanumeric charcters, hyphens, spaces

            N 1 Reply Last reply
            0
            • M mmagill0

              Just to let you know, you spelt characters wrong in the alert() function :)

              NeverHeardOfMe wrote:

              only alphanumeric charcters, hyphens, spaces

              N Offline
              N Offline
              NeverHeardOfMe
              wrote on last edited by
              #6

              Ta.. I love typos - I do them all the time!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups