JS verification Upload File extension method collection

Source: Internet
Author: User
Tags rtrim

Instance 1

can only upload jpg image file instance

  code is as follows copy code

<form action= "*.jsp" method= "POST" onsubmit= "return Check ()" >
<input type= "file" name= "file" >
<input Type= "Submit" value= "Upload" >
</form>

JS code


Function Check2 ()
{
var file = document. Getelementsbyname ("File"). Value;
if (file== "")
{
Alert ("Please select File");
return false;
}
var strtemp = File.split (".");
var strcheck = strtemp[strtemp.length-1];
if (strcheck.touppercase () = = ' JPG ')
{
return true;
}else
{
alert (' Upload file type is not correct! ');
return false;
}
}

Verifying the extension of the uploaded file

The code is as follows Copy Code

<input type= "File" name= "file" id= "file" onchange= "Checkfilename (This)" >


function Checkfilename (ID) {
var Oktext =/txt|jpg|xml|zip/i;//Here is the allowed extension
var filename = id.value;//Gets the value of the current File file field
var newfilename = Filename.split ('. '); This is a point separating the file name, because the suffix must end with something.
NewFileName = newfilename[newfilename.length-1];//This is getting file suffix, Because the split is an array, so the last array is the suffix name of the file. Newfilename.length is the current length,-1 is the position of the suffix, because it starts at 0.
Start checking suffix
if (Newfilename.search (oktext) = = 1) {//search If not just returned-1. This if the newfilename is not in Oktext, then the type that is not allowed to upload.
var err=oktext.tostring ();//convert positive to character
var errtext=err.replace (/^/|/+ (. *)/g, "");//End With regular substitution/start and/I
var errtext=errtext.replace (/|/g, ","); Replace INTO,
Alert ("Please upload as" +errtext+ ");/prompt
Return
}

}

The above methods are very scalable, and I'll introduce you to a very strong display of code.

The code is as follows Copy Code

<script language= "JavaScript" >
<!--
Go to left space
function LTrim (s) {
Return S.replace (/^s*/, "");
}

Go to the right space
function RTrim (s) {
Return S.replace (/s*$/, "");
}

Go to the left and right space
function Trim (s) {
return RTrim (LTrim (s));
}

Get file name extension
function Get_ext (F_path) {
var ext = ';

if (F_path!= null && trim (f_path). length > 0) {
F_path = Trim (F_path);
ext = f_path.substring (F_path.lastindexof (".") + 1, f_path.length);
}

return ext;
}

Verifying file name extension
function Chk_ext (F_path) {
var ext = get_ext (F_path);

Customized according to requirements
var accept_ext = new Array ("Doc", "PDF", "BPM", "JPEG", "JPG", "gif", "ppt", "xls");

var flag = false;

if (ext!= ') {
for (var i=0; i<accept_ext.length; i++) {
if (ext = = Accept_ext[i])
Flag = true;
}
}

return flag;
}
-->
</SCRIPT>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.