JavaScriptから画像のアップロードファイルの高さと幅を検証しようとしています。 falseを返しても動作しません。Javaスクリプトでの画像の幅と高さの確認 - Codeigniter
Javascriptのコード:
function Upload() {
var fileUpload = document.getElementById("userfile");
var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(.jpg|.png|.gif)$");
if (regex.test(fileUpload.value.toLowerCase())) {
if (typeof(fileUpload.files) != "undefined") {
var reader = new FileReader();
reader.readAsDataURL(fileUpload.files[0]);
reader.onload = function(e) {
var image = new Image();
image.src = e.target.result;
image.onload = function() {
var height = this.height;
var width = this.width;
if (height < 227 || width < 300) {
alert("Height and Width should be above 227px by 300px.");
return false;
}else{
alert("Uploaded image has valid Height and Width.");
return true;
}
};
}
} else {
alert("This browser does not support HTML5.");
return false;
}
} else {
alert("Please select a valid Image file.");
return false;
}
}
HTML:
<form name="add_projects" id="add_projects" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Project Name</label>
<input type="text" name="project_name" id="project_name" class="form-control" value="<?php if(!empty($project)){ echo $project->project_name;}?>">
<input type="hidden" name="id" id="id" value="<?php if(!empty($project)){ echo $project->id;}?>">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Project Sub Category Name</label>
<input type="text" name="project_sub_name" id="project_sub_name" class="form-control" value="<?php if(!empty($project)){ echo $project->project_sub_name;}?>">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Tags</label>
<input type="text" name="tags" id="tags" class="form-control" value="<?php if(!empty($project)){ echo $project->tags;}?>">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Poject Description</label>
<textarea id="editor1" name="project_description" class="form-control" rows="10" cols="80"><?php if(!empty($project)){ echo $project->project_description;}?></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Current Image</label><br>
<?php if(!empty($project) && $project->image !=""){?>
<img height="227" width="450" src="<?=base_url()?>assets/images/projects/<?=$project->image?>">
<?php }else{ echo "No Image Selected";} ?>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>New Image </label>
<div id="preview" style="width:100%"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Select Image </label>
<input type="file" name="userfile" id="userfile">
<input type="hidden" name="old_file" value="<?php if(!empty($project)){ echo $project->image;}?>">
<input type="hidden" name="id" value="<?php if(!empty($project)){ echo $project->id;}?>">
<span class="error">(Image should be more then 300px in width and 227px in height or above)</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<input type="submit" onclick="return Upload();" name="submit" id="submit" value="Save Changes" class="btn btn-primary">
</div>
</div>
</div>
</form>
フォームが送信されますし、画像の高さと幅は、以下のピクセルを指定した場合でも、フォームの送信は停止されません。..
どこが間違っていますか?
フォーム(jquery.validate.min.js)に対してもJquery Validationを使用しています。
ブライアンに感謝します。ありがとうございました。 –