www.webdeveloper.com
Results 1 to 5 of 5

Thread: JS file upload whitespace (space) check

  1. #1
    Join Date
    Aug 2007
    Posts
    7

    JS file upload whitespace (space) check

    Hi guys,

    I am trying to find out if there is a way of checking if a file a file that I am trying to upload has any spaces in its name. If true then I just want to display an alert telling the user to rename the file.

    Can this be acheived in JS and if so can anyone point me in the right direction?

    Thanks

  2. #2
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Something like this
    HTML Code:
    <form onsubmit="return validate(this);">
      File<br />
      <input type="file" name="file" value="" /><br />
      <input type="Submit" value="Submit" />
    </form>
    Code:
    function validate(form)
      if (/\s/.test(form.elements.file.value)) {
        alert('The filename can not contain whitespace. Please rename the file.');
        return false;
      }
      return true;
    }

  3. #3
    Join Date
    Aug 2007
    Posts
    7
    Thanks for the reply Dok.

    MY Firebug gives me an error on this line:

    if (/\s/.test(form.elements.file.value)) {

    Any ideas to why?

  4. #4
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Missed a curly bracket...
    Code:
    function validate(form) {
      if (/\s/.test(form.elements.file.value)) {
        alert('The filename can not contain whitespace. Please rename the file.');
        return false;
      }
      return true;
    }

  5. #5
    Join Date
    Aug 2007
    Posts
    7
    Stupid me. It's late. Thanks a lot for the help mate.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles