Forum Moderators: open

Message Too Old, No Replies

Form Validation Part Two

Using RegExp to Improve Code

         

scuba_fan

9:46 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



I posted earlier on validating a form with javascript and have some more valid and operational code that I'll love to improve and in the process, improve my own skills.

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...

DrDoc

10:36 pm on Mar 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to escape the period as well, or else it will match any character.
And, remove the caret...

filetype = /\.(gif¦jpg¦mtf¦jar¦jad¦mid¦mp3)$/i

if(!filetype.test(sFile)) {
//bad extension
}

ajkimoto

11:13 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



scuba_fan,

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

scuba_fan

11:21 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



DrDoc-
Thanks. That's the trick. I combined my learning from the previous thread with this and here's what I came up with...

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!

scuba_fan

11:25 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



ajkimoto-
Your headed right where I was going! Yes, I would seperate out the if statements so the user could see their mistake.

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...

scuba_fan

12:20 am on Mar 24, 2004 (gmt 0)

10+ Year Member



OK. Here's the combined jewel...Without your help here this would not have been within my capability...

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;
}