Forum Moderators: open
I've stayed away from RegExp for a while, just seemed too complex. But I'm learning, and the best way is to learn from those that have done it before...
Here is some working code, but rather inefficient I might say...
It takes a form field input and attempts to validate that it is only of a particular file type.
if((formDOMObj.attach1.value.lastIndexOf(".jpg")==-1) && (formDOMObj.attach1.value.lastIndexOf(".gif")==-1) && (formDOMObj.attach1.value.lastIndexOf(".mp3")==-1) && (formDOMObj.attach1.value.lastIndexOf(".3gp")==-1) && (formDOMObj.attach1.value.lastIndexOf(".mtf")==-1) && (formDOMObj.attach1.value.lastIndexOf(".mid")==-1) && (formDOMObj.attach1.value.lastIndexOf(".jad")==-1) && (formDOMObj.attach1.value.lastIndexOf(".jar")==-1))
{
alert("You can upload only GIF, JPG, ,JAR, JAD, MP3, 3GP, MTF and MID files");
return false;
}
See, Ugly! and a side effect is that its case sensitive! So a valid file type but with an uppercase extension is going to fail the test. Bummer.
I was playing with a RegExp that looks like this...
filetype = /^.(gif¦jpg¦mtf¦jar¦jad¦mid¦mp3)$/i
I think I have the RegExp right, but have no idea how to work it into the if statement test and get rid of all that && stuff...
Thanks in advanced for any tips...
How about this:
<script type="text/javascript">
<!--
//regular expression to check for file extensions
var RegFileType = /\.(gif¦jpg¦mtf¦jar¦jad¦mid¦mp3)$/gi
//regular expression to check for spaces and apostrophes
var RegSpaces = /[ ']+/
function checkit(){
myval=document.getElementById('myText').value
//if filename passes the extension test and fails the RegSpaces test
//(extension IS found and there are NOT spaces or apostrophes)
if(RegFileType.test(myval)&& RegSpaces.test(myval)==false){
alert('filename OK')
}else{
alert(filename bad')
}
//-->
</script>
<body>
<input type="text" id="myText" value="" />
<button onclick="checkit()">check it</button>
</body>
You could separate the if statement out into two in order to give the users more information (i.e. bad filename vs. bad file extension).
ajkimoto
filetype = /\.(gif¦jpg¦mtf¦jar¦jad¦mid¦mp3¦3gp)$/i
if (formDOMObj.attach1.value!= ""){
var sFile=(formDOMObj.attach1.value)
while( sFile.indexOf('/')!=-1){
sFile=sFile.replace('/','\\')}
var path=sFile.split('\\')
if(!filetype.test(path[path.length-1]))
{
alert("You can upload only GIF, JPG, ,JAR, JAD, MP3, 3GP, MTF and MID files");
return false;
}
}
compare that to the old one and this is much much cleaner!
I was just about to put it together, but alas you are much faster than I.
I've already declared the function and call it from the form.
I test for return false statements all the way through and if none are found, I pass the return true result back to the form for submission.
So I'll look more closely at your script...
Thanks again to those that contributed...
var RegFileType = /\.(gif¦jpg¦mtf¦jar¦jad¦mid¦mp3¦3gp)$/i
var RegValidFile = /[' ]/
if (formDOMObj.attach1.value!= ""){
var sFile=(formDOMObj.attach1.value)
while( sFile.indexOf('/')!=-1){
sFile=sFile.replace('/','\\')}
var path=sFile.split('\\')
if(!RegFileType.test(path[path.length-1])){
alert("You can only upload Wallpapers(GIF & JPG), MIDI, MP3, Movies(3GP) and JAVA Games");
return false;
}
if(RegValidFile.test(path[path.length-1])){
alert("PLEASE READ THE IMPORTANT NOTICE!\n\n NO SPACES or APOSTROPHES ALLOWED\n\n Rename your file and try again");
return false;
}
}
return true;
}