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.