Today is Sunday
September 5, 2010

Search Results Tag: javascript

August 29, 2010

How to Check and Validate a file extension with JavaScript / jQuery

From time to time comes the need in almost every coders life to use an upload form on a page they are working on. Be it a single file upload form or a full form with contact information. In either event 99% of the time the case will be that the coder is going to want to double check the extension type. Grant it your going to want to check more than just that when you offer the ability to upload a file on to any server you are running a site on. To prevent a malicious user from attempting to hijack your site. I do feel however that it is worth mentioning that this only one of at very least several precautions you should take. This function alone will not secure your uploads 100%

The Function:

function checkExtension() {
  var extension = new Array(".png",".gif",".jpg",".jpeg",".tif",".pdf");
  var fieldvalue = $('#thefile').val();
  //var fieldvalue = document.formName.fieldName.value;
  var thisext = fieldvalue.substr(fieldvalue.lastIndexOf('.'));
    for(>var i = 0; i < extension.length; i++) {
      if(thisext == extension[i]) { alert("File type: Allowed"); return >true; } else { alert("File type: Not Allowed"); return >false; }
    }
}

Example of use:

<form action="#none" name="testUpload">
<input type="file" name="myfile2up" value="" /> <input value="Upload" type="button" onClick="checkExtension(); return false;" />
</form>


Demo:

Extra information:
The actual upload portion of this script has been disabled, this form will not upload anything it is for demo purpose only.

Allowed file types in this demo are:
".png",".gif",".jpg",".jpeg",".tif",".pdf"

June 27, 2010

Javascript vs PHP

by Chris — Categories: Coding, Javascript, PHP — Tags: , , , 7 Comments

This is a question I often see and get. “Which one should I use, JavaScript or php?” Well in the end it all boils down to what your doing and how your are expecting it to handle. The biggest key differences between JavaScript and PHP is one runs or renders rather while the page is loading. While the other can do stuff to a page while its loading or after it has loaded.

Both languages are very similar in construct or at least they are in my opinion, and you write code almost the same way. Obviously they have different built in functions, and names for the functions, and they differ here and there. Generally you can almost do anything with JavaScript that you can with PHP and vice versa.

Now comes again the question “Which should I use?”. Well I personally like PHP for a wide array of things and will generally use it over JavaScript when possible however with todays technology always trying to push the bar up and tempting to break the barriers of just how far you can push someones browsers abilities I do favor AJAX which is the use of PHP and JavaScript together. I say “when possible” because there are times where it definitely is not wise to use AJAX. But thats only due to security concerns. But worry not, most applications you will build with AJAX if you use it wisely and properly you wont have much to worry about. Which one day I am sure I will write an article or many about that.

If you want your elements to be flashy and less bandwidth consuming I would suggest using JavaScript where and when possible to handle your pages content and how its loaded, then handled there after. If bandwidth isn’t much of an issue for you which with most hosting providers it isn’t (unless your pushing several thousands of gigs of there bandwidth a month, then you should check the fine print in there contracts). Then handle everything via PHP its a bit more static, pages have to load but if used properly its as secure as it gets, and can run pretty fast as well.

If your interested in going the AJAX route which I highly recommend I would suggest using jQuery. As its the most stable and mature of frameworks out there and has a much larger community of developers to help get answers from if you ever get stuck.

Are you recently starting out with php, javascript, ajax, mysql, or really any web related scripting language? If you are might I suggest to you a site that helped even me learn the basics years upon years ago. http://www.tizag.com they don’t cover everything, but they cover more than just the simple basics and can give you a good footing on any given scripting language.

AJAX ready frameworks that you can choose from in no particular order (obtained from Wikipedia):

jQuery
jQuery UI (used with jQuery)
Prototype
Script.aculo.us (used with prototype)
Ext (now known as Sencha)
MooTools
Yahoo! UI Library
Dojo Toolkit

Quick Question I see with these framworks mostly due to people searching for ready made scripts is can you use more than one framework. Well ultimately it depends which you use. I know jQuery (even combined with UI) you can set it up to allow other frameworks as mentioned on the jQuery site in there article/post about the subject Using jQuery with Other Libraries however outside of that I cant really vouch for the other libraries and there support on the subject as I prefer jQuery and use that over the rest when ever possible (unless I am working on a clients site and they have something different from jQuery)

© 2010 Monkey Tooth Productions All rights reserved - Wallow theme by TwoBeers Crew - Powered by WordPress - Have fun!