473,322 Members | 1,405 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,322 software developers and data experts.

Dynamic "match" / regex?

Hi there

Ideally, I'd like to create one javascript function and pass the file
extension i'm looking for to see if its there:

<input type="file" onchange="checkFile('pdf',this)" />

and then have the function something like (w/ Prototype)
function checkFile(theExtension,theField) {
var field = $(theField);
var fieldName = field.name;
var fieldID = field.id;
var fieldValue = field.value;
var matchPattern = "/\."+theExtension+"$/im";
if (fieldValue.match(matchPattern)) {
alert('Its a '+theExtension+'!');
} else {
alert('no!');
}
}
The problem is, the line with "fieldValue.match" is not using the "var
matchPattern" value (my RegEx expression)...

Can anyone make this work / must i use 'eval' to get the match to work
against the dynamic var I created?

Jul 3 '07 #1
3 2447
Good Man <he***@letsgo.comwrote in news:Xns9962A252BF8F8sonicyouth@
216.196.97.131:
Hi there

Ideally, I'd like to create one javascript function and pass the file
extension i'm looking for to see if its there:

<input type="file" onchange="checkFile('pdf',this)" />

yes, the form field is properly made in real life:

<input type="file" id="myFile" name="myFile" onchange="checkFile
('pdf',this)" />
Jul 3 '07 #2
On Jul 4, 5:56 am, Good Man <h...@letsgo.comwrote:
Hi there

Ideally, I'd like to create one javascript function and pass the file
extension i'm looking for to see if its there:

<input type="file" onchange="checkFile('pdf',this)" />

and then have the function something like (w/ Prototype)

function checkFile(theExtension,theField) {
var field = $(theField);
var fieldName = field.name;
var fieldID = field.id;
var fieldValue = field.value;
var matchPattern = "/\."+theExtension+"$/im";
Try:

var matchPattern = new RegExp('\\.' + theExtension + '$','im');

When constructing a regular expression this way you must quote
backslashes, hence \\, so \b becomes \\b, \n becomes \\n, etc.
if (fieldValue.match(matchPattern)) {
If all you are doing is matching the RegExp, use test:

if (matchPattern.test(fieldValue)) {
--
Rob

Jul 3 '07 #3
RobG <rg***@iinet.net.auwrote in news:1183495647.365626.100600
@j4g2000prf.googlegroups.com:
> var matchPattern = "/\."+theExtension+"$/im";

Try:

var matchPattern = new RegExp('\\.' + theExtension + '$','im');

When constructing a regular expression this way you must quote
backslashes, hence \\, so \b becomes \\b, \n becomes \\n, etc.
> if (fieldValue.match(matchPattern)) {

If all you are doing is matching the RegExp, use test:

if (matchPattern.test(fieldValue)) {

Many thanks RobG...
Jul 3 '07 #4

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

Similar topics

5
by: kmunderwood | last post by:
I am trying to combine "if match=" and "when test" I am a newbie, and have made both work separately, but I can not seem to combine them. This is my xml("index.xml")page(I can not change this,...
7
by: Chris Mantoulidis | last post by:
I was wondering if there was something like a "match" class in the std. By match class I mean something like a search tool. That is I will tell it in what text to search and WHAT to search for. The...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression...
1
by: sonia93 | last post by:
Hi , I am having a variable which has "++" or "+" as part of it . My file consists something like this air++ water fire+ I want to go one by one to each line and match a particular word...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.