我在 Internet Explorer 中遇到表单验证问题。
在以下脚本中,我在 中创建了一个表单HTML
,并编写了 JavaScript 逻辑以在提交表单之前执行表单验证。
该脚本正确验证表单并仅在 Chrome 和 Firefox 中提供警报,但不在 Internet Explorer 中,我想知道为什么?:
http://jsfiddle.net/PKLQn/121/
var error="";
function Checkfiles() {
var fup = document.getElementById('flUpload');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
var chkext = ext.toLowerCase();
//var mime = fup.type;
//if (mime=='image/png' || mime=='image/jpeg' || mime=='image/gif') {
if(chkext=="gif" || chkext=="jpg" || chkext=="jpeg" || chkext=="png") {
return true;
} else { return false; }
} // Checkfiles
function Checksize() {
var iSize;
if ($("#flUpload")[0].files[0]){
iSize = ($("#flUpload")[0].files[0].size / 1024);
}
if(Checkfiles()==true) {
if (iSize < 51200.00) {
return true;
}
else{
return false;
}
} else { error += "- Upload GIF, PNG, JPG images only, smaller than 50 KB."; return false; }
} //Checksize
function Checkfields() {
// Validate Email
var email = $("#email").val();
if (/(.+)@(.+){2,}\.(.+){2,}/.test(email)) { } else { error += "- Please enter a valid email address (eg. http://youwebsite.com)\n"; }
// Validate Title
var title = $("#title").val();
if (title=="" || title==null) { error += "- Please enter a valid title for your advertisement (Max. 60 chars.)\n"; }
// Validate URL
var url = $("#url").val();
if (/^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/.test(url)) { } else { error += "- Please enter a valid URL.\n"; }
}// Checkfields
function newfuncion() {
var fields = Checkfields();
var size = Checksize();
var files = Checkfiles();
if(fields==true && size==true && files==true) {
return true;
} else {
if(error!=""){alert(error); error=""; return false;} else {return true;}
return false;
}
} //Juntar todas las funciones
input[type="text"] {
display: inline-block;
padding: 3px 8px;
margin: 5px 0px 5px 10px;
}
label {
font-weight: bold;
cursor: pointer;
}
.submit-btn {
background: #4CAF50;
border: none;
color: #fff;
padding: 8px 15px;
text-align: center;
font-size: 13px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="fff1" onsubmit="return newfuncion();">
<label for="email">Email: </label> <input type="text" placeholder="Email" id="email" /><br/>
<label for="title">Title: </label> <input type="text" placeholder="Title" id="title" /><br/>
<label for="url">URL: </label> <input type="text" placeholder="URL" id="url" /><br><br>
<input type="file" id="flUpload" /><br/><br>
<input type="submit" class="submit-btn" value="CONTINUE" />
</form>
我想知道为什么我的代码不能在 Internet Explorer 8、9 上运行?