Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get filename from browse and put in title box problem 1

Status
Not open for further replies.

AndyH1

Programmer
Jan 11, 2004
350
GB
I have a javascript (or Ajax) problem in some asp.net stuff I'm developing but wonder if anyone can advise

Iv'e got a form for uploading files in which the user can enter a title and select a file (sometimes they want a more meaningful name than the filename). In asp.net this is

<form ...>
<asp:textbox id="txtTitle" runat="server"></asp:textbox>
<input id="myFile" type="file" name="myFile" runat="server"/>
</form>

which interpretes in standard html as:
<form action="" ...>
<input type="txtTitle" name="title">
<input type="file" name="myFile">
<button id="submit" ..>
</form>

The myFile shows a browse button which allows you to go select a file (pretty straightforward so far) before pressing submit

What I'd like to do though is if the user uses the browse button to select a file and hasn't put in a title in the title field then the filename is put in the title field.

Obviously its easy to do this when you submit, but I'd like to do it dynamically when the user has selected a file, giving the user the choice of editing the text before submitting but saving them having to type in the whole filename. Is there anyway of doing this - either in Ajax or javascript?

Thanks
AndyH1


 
Hi

Like this ?
Code:
<form action="" ...>
<input type="txtTitle" name="title">
<input type="file" name="myFile" [red]onchange="if(!this.form.title.value)this.form.title.value=this.value.replace(/.*\//,'')"[/red]>
<button id="submit" ..>  
</form>



Feherke.
 
Thanks feherke,
Thats just what I need.
Many thanks
AndyH1
 
feherke,

Is there anyway of just extracting the files title? ie

'first spreadsheet' from

C:\\Documents\Mydocs\first spreadsheet.doc

I assume its the regular expression /.*\// but haven't been able to figure this or how it fits in this.value.replace(/.*\//,'')", guess need to find last \?

AndyH1
 
Hi

You can say "either of the enumerated characters" by enumerating them between brackets ( [] ). So to match either slash ( / ) or backslash ( \ ) :
Code:
string.replace(/.*[/\\]/,'')
In your example you cut off the extension too. For that you can modify the expression like this :
Code:
replace(/.*[/\\]([^.]+).*/,'$1')
Although I think you not want this.

Feherke.
 
Feherke,
Thats great - many thanks.
Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top