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;
    })
})
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
nksingh420
  • 97
  • 1
  • 1
  • 6
  • Use `this` instead of DOM-query to get your current file input. – Tommi Dec 02 '13 at 08:47
  • 3
    this question get the file name with extension but my question is get file name without extension !!!!! – nksingh420 Dec 03 '13 at 04: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? – twiz Mar 17 '14 at 05:09
  • 2
    Definitely **not** a duplicate. – Michael Whatcott Jan 19 '15 at 21:46
  • document.location.pathname.replace(/(.*[\/\\]+|\.\w+$)/gm, ''); –  Jun 22 '15 at 18:57

2 Answers2

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/

Andrew
  • 5,290
  • 1
  • 19
  • 22
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.
Community
  • 1
  • 1
AurA
  • 12,135
  • 7
  • 46
  • 63
  • `uploadcontrol.split.length` what kind of black magic is it? – Tommi Dec 02 '13 at 08:50
  • @Tommi It is JavaScript black magic.. which provides us with split function. Read about it http://www.w3schools.com/jsref/jsref_split.asp , we get a list of string based on no of dot(.) – AurA Dec 02 '13 at 08:54
  • Yes, but how you can get `length` of function itself, and what it does for you? Did you tried to run your code? – Tommi Dec 02 '13 at 08:57
  • Ok, I tried and it works. But I don't get this trick, I will read more. – Tommi Dec 02 '13 at 08:59
  • @Tommi The function returns an array so that is valid http://www.w3schools.com/jsref/jsref_length_array.asp – AurA Dec 02 '13 at 08:59
  • The revelation for me was `String.prototype.split` contains proper `length` of array which was created when `split` was called. That's cool. – Tommi Dec 02 '13 at 09:01
  • ... Or it's not. It's always 2. Anyway, I see you edited your answer, and now I see how it works. – Tommi Dec 02 '13 at 09:04