-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage upload validation using js.html
More file actions
49 lines (43 loc) · 1.29 KB
/
image upload validation using js.html
File metadata and controls
49 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>image validation</h1>
<form name="myform" onsubmit="return validation()">
<label >Image upload</label>
<input type="file" name="img_upload">
<br><br>
<input type="submit" value="submit">
</form>
<script>
var img=document.forms['myform']['img_upload'];
var validExt=["jpeg","png","jpg"];
function validation(){
if (img.value!=='') {
var imgExt=img.value.substring(img.value.lastIndexOf('.')+1);
var result=validExt.includes(imgExt);
if (result==false) {
alert("selected files are not image");
return false;
}
else{
if(parseFloat(img.files[0].size/(1024*1024))>=1){
alert("image size must be less than 3MB : size is"+parseFloat(img.files[0].size/(1024*1024)));
return false;
}
}
}
else{
alert("no image is selected");
return false;
}
return true;
}
</script>
</body>
</html>