7

i am going to upload file using file upload control in asp.net . now i want to get file name without extension using java script . i want to validate file name should be only integer format

$(function () {
    $('#<%=File1.ClientID %>').change(function () {
         var uploadcontrol = document.getElementById('<%=File1.ClientID%>').value;
    })
})
5
  • Use this instead of DOM-query to get your current file input. Commented Dec 2, 2013 at 8:47
  • 3
    this question get the file name with extension but my question is get file name without extension !!!!! Commented Dec 3, 2013 at 4:27
  • 2
    The "duplicate" question is not asking the same thing. This question asked for the extension to also be removed. Why does everyone on SO seem to go around closing every question they come across without even reading them? Commented Mar 17, 2014 at 5:09
  • 2
    Definitely not a duplicate. Commented Jan 19, 2015 at 21:46
  • document.location.pathname.replace(/(.*[\/\]+|\.\w+$)/gm, ''); Commented Jun 22, 2015 at 18:57

2 Answers 2

11

Try this:

$('#test').change(function() {

      //something like C:\fakepath\1.html 
    var fpath = this.value;

    fpath = fpath.replace(/\\/g, '/');

    var fname = fpath.substring(fpath.lastIndexOf('/')+1, fpath.lastIndexOf('.'));

    console.log(fname);

});

http://jsfiddle.net/rooseve/Sdq24/

Sign up to request clarification or add additional context in comments.

Comments

3

You can split it with respect to last instance of dot('.')

var uploadcontrol = document.getElementById('<%=File1.ClientID%>').value;

uploadcontrol = uploadcontrol.split('.')[uploadcontrol.split('.').length - 2];

But,

this is valid for simple case only... a much better generic answer is given at How to get the file name from a full path using JavaScript?

Take care of these things in a generic solution

  • get rid of / or go to the last /
  • if string has multiple dot('.') then only remove the string after last dot.

7 Comments

uploadcontrol.split.length what kind of black magic is it?
@Tommi It is JavaScript black magic.. which provides us with split function. Read about it w3schools.com/jsref/jsref_split.asp , we get a list of string based on no of dot(.)
Yes, but how you can get length of function itself, and what it does for you? Did you tried to run your code?
Ok, I tried and it works. But I don't get this trick, I will read more.
@Tommi The function returns an array so that is valid w3schools.com/jsref/jsref_length_array.asp
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.