title: Javascript validate filename before upload
body: <p>I simply want to validate the filename of the image being uploaded to ensure that it does not have spaces or unusual characters.</p>
  
  <p>This is my latest attempt from searching around, still no luck. Could it be something to do with the path of the file? is it taking this or just the file name into account?</p>
  
  <p>I have this and a check of the extention working server side with php, but I would like a prompt to the user before submitting.</p>
  
  <p>At this point in time im getting the alert pop up even whether i use a file name it should accept or one that it should reject.</p>
  
  <p>JavaScript</p>
  
  <pre><code>function validate(elem){
      var alphaExp = /^[a-zA-Z_-]+$/;
      if(elem.value.match(alphaExp)){
          return true;
      }else{
          alert("File name not suitable");
          elem.focus();
          return false;
      }
  }
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;label for="file"&gt;Filename:&lt;/label&gt;
  &lt;input type="file" name="filename" id="filename" onchange="validate(this)" /&gt; 
  &lt;p&gt;&lt;input type="submit" name="submit" class="submit" value="Submit" /&gt;
  &lt;/form&gt;
  </code></pre>