473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Checking for a valid filename using Reg expressions - HELP!

Please can someone help me, I am trying to write a Regular Expression
check for a valid (Windows) filename (cant believe I couldnt find one
on the net already?!?!?!). Anyway, here is what I have but it doesnt
fully work - e.g. it allows the use of quotes, and the ampersand in
some positions and I just cant correct it??! Sorry for being a dull
newbie.

TIA.

-------------------------------------------------

RegExp = /^["']?(.)?([^\|\&\<\>\;\*\?\\\\\/:]+\.\w+)["']?$/

Jul 23 '05 #1
4 7581
pa************@gmail.com wrote:
Please can someone help me, I am trying to write a Regular Expression
check for a valid (Windows) filename (cant believe I couldnt find one
on the net already?!?!?!). Anyway, here is what I have but it doesnt
fully work - e.g. it allows the use of quotes, and the ampersand in
some positions and I just cant correct it??! Sorry for being a dull
newbie.

TIA.

-------------------------------------------------

RegExp = /^["']?(.)?([^\|\&\<\>\;\*\?\\\\\/:]+\.\w+)["']?$/


File name or file path?
Mick
Jul 23 '05 #2
file name - path not required...

Jul 23 '05 #3
pa************@gmail.com wrote:
file name - path not required...

What are the rules for a Windows file name?
Mick
Jul 23 '05 #4
JRS: In article <11**********************@f14g2000cwb.googlegroups .com>
, dated Thu, 24 Feb 2005 11:01:45, seen in news:comp.lang.javascript,
pa************@gmail.com posted :
Please can someone help me, I am trying to write a Regular Expression
check for a valid (Windows) filename (cant believe I couldnt find one
on the net already?!?!?!). Anyway, here is what I have but it doesnt
fully work - e.g. it allows the use of quotes, and the ampersand in
some positions and I just cant correct it??! Sorry for being a dull
newbie. RegExp = /^["']?(.)?([^\|\&\<\>\;\*\?\\\\\/:]+\.\w+)["']?$/


For a start, the close quote must match the open quote. But only " can
be used for that.

RegExp = /^("?)(.)?([^\|\&\<\>\;\*\?\\\\\/:]+\.\w+)\1$/

I don't see why you allow a possible arbitrary character after the first
optional quote.

You appear to disallow the colon, meaning that you cannot specify a
drive, and you cannot specify a path. ISTM that in general the drive
and path should be allowed; include ([a-zA-Z]:)? and allow multiples
of what you have above with \ in between - which may be impossible.

You seem to require an extension; I'm not sure of that. In the
extension, you allow only word characters; an extension can, ISTM,
contain all the characters that the rest can - I frequently use $$$, for
example, and sometimes £££.

In your list of barred characters, I'd put colon before slash.
Javascript won't mind, but it will be a little easier to read.
If these are meant to be names of existing files, why bother? If the
file can be found, the name is OK; otherwise, it is wrong. If they are
meant to be names of new files, do you need to allow all possibilities?
you might list instead those that are needed, which has the benefit of
rejecting those of value above 126.
You should have given a written description of the intended criteria;
then it would have been easier to check your intent and your
implementation thereof.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: unndunn | last post by:
I'm trying to design a regular expression that matches (using preg_match()) when a string is a well-formed Email address. So far I have this: /^+@+\.{2,4}$/i I got that from...
7
by: - ions | last post by:
I have created a JComboBox with its Items as a list of "M" numbers ie. M1,M2,M3.......throgh too M110 (thes are the messier objects, a catolouge of deep sky objects) the user selects of of these...
3
by: Guy Robinson | last post by:
I have the code below which parses an expression string and creates tokens. Can anyone suggest the best of error checking for things like: Valid variable only obj.attribute -whitespace allowed...
2
by: Xam | last post by:
Hello everybody Do you know of a javascript routine that can warn if there are any pre-defined invalid chars in the filename of an INPUT file box before it is submitted with the submit button. ...
7
by: Jim Cook | last post by:
We have a macro which takes various index constants as an argument and offsets into an array. The macro can be an Lvalue or Rvalue. The index is not zero based. I would like a compile time error...
3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
14
by: tranky | last post by:
Hi, i'm italian...so...excuse me for my english. I've a little problem....in what manner i can check a textbox for know if it contain only character from A-Z (a-z), numbers (0-9), and underscore...
5
by: emma_middlebrook | last post by:
Hi I've found the rules that cover what is a valid attribute name here: http://www.w3.org/TR/2006/REC-xml-20060816/ 2.3 Common Syntactic Constructs Is there anything already in the dotnet...
12
Kelicula
by: Kelicula | last post by:
I am trying to ensure that only files of a certain type can be uploaded. Why doesn't this work?? (It's a code snippet..not the whole file) if(my $file = $q->param('avatar')){
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.