JS get to judge upload file input=file suffix name example

Source: Internet
Author: User

Example

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<input type= "File" id= "Fileid" onchange= "Selectfile (This)"/>
</body>
<script type= "Text/javascript" >

function Selectfile (input) {

var fileName = Input.value;
if (Filename.length > 1 && fileName) {
var Ldot = Filename.lastindexof (".");
var type = filename.substring (Ldot + 1);

if (Type!= "img") {
alert (type);
Clears the currently selected file Www.111cn.net
Input.outerhtml=input.outerhtml.replace (/(value= "). +"/I, "$");
}
}
}

</script>

Example

The code is as follows Copy Code
<form action= "uploadfile.php" enctype= "Multipart/form-data" method= "post" target= "Iframeupload" >
<iframe name= "Iframeupload" "src=" "width=" "Height= frameborder=0" "No." Scrolling= "no" style= "Display:none" >
</iframe>
<input id= "Test_file" name= "Test_file" id= "Fileid" type= "file" onchange= "Selectfile (This)" >
<input value= "Upload file" type= "Submit" >
</form>
<script type= "Text/javascript" >
function Selectfile (fnupload) {
var filename = fnupload.value;
var mime = filename.tolowercase (). substr (Filename.lastindexof ("."));
if (mime!= ". jpg")
{
Alert ("Please select JPG photo upload");
fnupload.outerhtml=fnupload.outerhtml;
}
}
</script>

Example

  code is as follows copy code

function Check_filetype (str) {

var pos = Str.lastindexof (".");

var LastName = str.substring (pos,str.length)//Where the file suffix name is also available in array mode to obtain Str.split (".")

if (Lastname.tolowercase ()!= ". jpg" && lastname.tolowercase ()!= ". gif") {

Alert ("The file type you uploaded is" +lastname+ "and the www.111Cn.net picture must be. Jpg,.gif type");

Document.myform.pic.focus ();

return false;

} else {

return true;

}

}

Example

  code is as follows copy code

function LastName () {
Get the file path you want to upload
var filepath = document.getElementById ("File1"). Value;
To avoid a problem with the escape backslash, this will be converted
var re =/(\+)/g;
var filename=filepath.replace (Re, "#");
Cut the path string to intercept
var one=filename.split ("#");
Gets the last of the array, that is, the file name
var two=one[one.length-1];
Then intercept the filename to get the suffix name
var three=two.split (".");
Gets the last string of the Intercept, which is the suffix name
var last=three[three.length-1];
Add a suffix name type that needs to be judged
var tp = "Jpg,gif,bmp,jpg,gif,bmp";
Returns the position of a qualifying suffix name in a string
var rs=tp.indexof (last);
If the result returned is greater than or equal to 0, the file type that contains the allowed upload is indicated
if (rs>=0) {
return true;
}else{
Alert ("The upload file you selected is not a valid picture File!") ");
return false;
}
}

Some friends like to use onclick so the user if the Click button is can be judged, but the direct press ENTER on the detection is not, so we just add onsubmit= on the form "return LastName ()" can be.

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.