View Full Version : using regular expressions to find the single qoute character
solburn
09-18-2002, 04:24 PM
Hi All!
I'm totally frustrated... trying to validate an <input type="file"> form field. I want to prevent files with the single quote (') character and alert the user to change the filename.
Here is what I have:
function checkFile(form)
{
var regex = /\'/g;
var sValue = document.forms.frmDetails.fPath.value.search(regex);
if (sValue > 0)
{
window.alert("Found");
return false;
}
else
{
window.alert("Not Found");
return false;
}
}
I've tried these too:
regex = /'/g;
regex = /'*/g;
I've even tried these:
var sCode = String.fromCharCode(39);
regex = /String.fromCharCode(39)/g;
regex = new RegExp(sCode, "g");
I can find any character except the single quote... :confused:
Thank you in advance!
-Sol
solburn
09-18-2002, 05:14 PM
There is a lot of Jedi Mind Tricks in this one...
Basically encoded the string using the escape() function, then searched for the escape characters.
Also found out that two different encoded character sets appear depending on how the file name is entered. If a user browes the files and chooses a file that way, a single (') came back w/an encoded value of (%u2019), but if its typed it then it used the normal encoding of (%27).
Here's the code:
function checkFile(form)
{
var sValue = document.forms.frmDetails.fPath.value;
var sEncoded = escape(sValue);
var iValue1 = sEncoded.indexOf("%u2019");
var iValue2 = sEncoded.indexOf("%27");
window.alert(sEncoded);
if (iValue1 == -1 && iValue2 == -1)
{
window.alert("Not Found");
return false;
}
else
{
window.alert("Found");
return false;
}
}
Thank you to all who read the post!
-Sol
mordred
09-19-2002, 12:33 AM
May the source be with you, but...
... I believe there is no need for Jedi tricks. To me it appears that your logic in your checkForm function is flawed: You use String.search(), which returns you the position of the first appearance of the regexp search pattern in the string, starting at 0 for the first position, and returning -1 if no matches are found.
So basically when you have a single quote as the first character of your text field, it won't reported as found... with a slight adjustment your code above worked for me in Moz1.0, IE5.5 and NS4.7 on Win2k.
Because there are some Regexp quirks depending on browser versions/vendors/OS's, you might have actually run into one, but for this case you should try my suggestion and if that doesn't help, tell us which browser in which environment went to the dark side...
solburn
09-19-2002, 06:58 AM
Thank you for the reply!:thumbsup: I'll try adjusting the code and actually check for [== -1] instead of [> 0].
Here's some more information...
To start the file name is:
Laura's Vacation Pics.ppt
and when I use <input type="file" name="fileName"> to browse, the value that is in the form field is actually:
c:\My Pictures\Laura's Vacation Pics.ppt
My users have either HP-UX/Netscape 4.7 or above Windows/IE4 or above.
I was testing on Win2k w/IE 5.5
I think the issue comes up because the input type is "file". Somehow, the single quote (') is not the normal quote. It's some other kind of quote. Sounds weird, but when I "escaped" it I got 2 different values for the quote depending on how it was entered into the form field. I think that might be the real cause.
Anyway, what tweaks did you have to do to get it to work on:
Moz1.0, IE5.5 and NS4.7.
Let me know when you get a chance! THANKS again.
-Solburn
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.