?
Solved

How do i do a regex for file extension and file path form validation to use in javascript

Posted on 2009-02-12
3
Medium Priority
?
1,529 Views
Last Modified: 2012-05-06
Hello,
does anyone know how to make file extension validation regex to use with javascript note: my users upload photo from their harddisk so we need to validate the path

Thank you
0
Comment
Question by:MrFahad
3 Comments
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 80 total points
ID: 23622411
could you please assign more points?
<form onsubmit="return validateHeight(this);">
<input type="text" name="imagename">
<input type="submit" value="validate extension">
</form>
 
 
<script language="javascript">
var pattern=new RegExp("\.(gif|jpg)$");
 
function validateHeight(formObj){
//  alert(pattern.test(formObj.imagename.value));
  return (pattern.test(formObj.imagename.value));
}
</script>

Open in new window

0
 
LVL 18

Accepted Solution

by:
Morcalavin earned 120 total points
ID: 23622438
What are the allowable extentions? JPG, GIF, PNG, BMP?  I'll assume all of them.

<form action="whatever" onsubmit="return validateExt(this)" enctype="multipart/form-data">
...all your form stuff.
<input type="file" name="file" id="file" />
</form>

//place this between your script tags
function validateExt(formToValidate)
{
  if(!formToValidate.file.value.match(/^.+\.(jpe?g|bmp|png|gif)$/i))
  {
     return false;
  }
  //do whatever else here
  return true;
}
//also test on your server side in case javascript is disabled!

Open in new window

0
 

Author Closing Comment

by:MrFahad
ID: 31546061
i used Morcalavin solution but bot solutions work great
thanks for the help guys :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question