简体   繁体   English

如何同时验证多个输入字段

[英]How do I validate multiple input fields at the same time

I'm trying to validate file type so only JPGs or PNGs can be submitted in my form. 我正在尝试验证文件类型,因此只能以我的形式提交JPG或PNG。 I've set it so onChange of the image upload field an alert pops up and the 'upload' button is hidden. 我已对其进行了设置,以便在“更改图片上传字段”时弹出警报并隐藏“上传”按钮。 However I have 5 fields and if I choose a correct filetype in another box the button is then shown even if the wrong filetype is still selected in another field. 但是,我有5个字段,如果在另一个框中选择了正确的文件类型,则即使在另一个字段中仍然选择了错误的文件类型,也会显示该按钮。 How can I clear the input field at the same time as triggering the alert if the filetype is wrong? 如果文件类型错误,如何在触发警报的同时清除输入字段?

I've tried this.value = ""; 我已经尝试过this.value =“”; between changing the class and the alert but I'm not sure of the correct syntax to clear the current box 在更改类和警报之间,但我不确定清除当前框的正确语法

  function validate(fName){ splitName = fName.split("."); fileType = splitName[1]; fileType = fileType.toLowerCase(); if (fileType != 'jpg' && fileType != 'jpeg' && fileType != 'png'){ document.getElementById("uploadbutton").className = "hide"; alert("You must select a .jpg or .png, file."); } else { document.getElementById("uploadbutton").className = "fwdbutton upload"; } } 
  <div id="upload"> <h2>If possible, please could you include photographs of the following:</h2> <p><label for='uploaded_file1'>Current boiler:</label> <input type="file" name="uploaded_file1" id="uploaded_file1" class="uploadfields" accept="image/png, image/jpg, image/jpeg" onChange="validate(this.value)"><a href="#" class="clearfile" id="clear1">X</a></p> <p><label for='uploaded_file2'>Gas meter:</label> <input type="file" name="uploaded_file2" id="uploaded_file2" class="uploadfields" accept="image/png, image/jpg, image/jpeg" onChange="validate(this.value)"><a href="#" class="clearfile" id="clear2">X</a></p> <p><label for='uploaded_file3'>Boiler pipe work:</label> <input type="file" name="uploaded_file3" id="uploaded_file3" class="uploadfields" accept="image/png, image/jpg, image/jpeg"onChange="validate(this.value)"><a href="#" class="clearfile" id="clear3">X</a></p> <p><label for='uploaded_file4'>Outside flue:</label> <input type="file" name="uploaded_file4" id="uploaded_file4" class="uploadfields" accept="image/png, image/jpg, image/jpeg"onChange="validate(this.value)"><a href="#" class="clearfile" id="clear4">X</a></p> <p><label for='uploaded_file5'>Anything else relevant:</label> <input type="file" name="uploaded_file5" id="uploaded_file5" class="uploadfields" accept="image/png, image/jpg, image/jpeg" onChange="validate(this.value)"><a href="#" class="clearfile" id="clear5">X</a></p><br /> <input class="backbutton showmoved" type="button" value="<< back" /> <input class="fwdbutton upload" id="uploadbutton" type="button" value="upload >>" /> <p><a class="nophotos" id="nophotos">I have no photos &gt;&gt;</a></p> </div> 

Many thanks for any advice, Helen 非常感谢您的建议,海伦

Please try this. 请尝试这个。

  var _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"]; function ValidateSingleInput(oInput) { if (oInput.type == "file") { var sFileName = oInput.value; if (sFileName.length > 0) { var blnValid = false; for (var j = 0; j < _validFileExtensions.length; j++) { var sCurExtension = _validFileExtensions[j]; if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) { blnValid = true; break; } } if (!blnValid) { alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", ")); oInput.value = ""; return false; } } } return true; } 
  File 1: <input type="file" name="file1" onchange="ValidateSingleInput(this);" /><br /> File 2: <input type="file" name="file2" onchange="ValidateSingleInput(this);" /><br /> File 3: <input type="file" name="file3" onchange="ValidateSingleInput(this);" /><br /> 

You can use the following regular expression to check whether the file valid. 您可以使用以下正则表达式来检查文件是否有效。

/\.(jpe*g|png)$/gi

And then you can use the test() method to check if the file is valid in your if statement. 然后,您可以使用test()方法在if语句中检查文件是否有效。

if(/\.(jpe?g|png)$/gi.test(s)) {
  //TODO
}

use a counter to see if you have more errors: 使用计数器来查看是否还有更多错误:

var counter= 0;
function validate(fName){

    splitName = fName.split(".");
    fileType = splitName[1];
    fileType = fileType.toLowerCase();
    if (fileType != 'jpg' &&  fileType != 'jpeg' && fileType != 'png'){
      alert("You must select a .jpg or .png, file.");
      counter = counter + 1;
    }
    if (counter !=0){
        document.getElementById("uploadbutton").className = "hide";
    }else{
        document.getElementById("uploadbutton").className = "fwdbutton upload";
    }
}

each time you run the function it will check if you have an error. 每次运行该功能时,它将检查您是否有错误。 This code otherwise is an example and doesn't handle if you fix a previously marked error. 否则,此代码为示例,如果您修复了先前标记的错误,该代码将无法处理。

My advice is to redesign the code to check each input once on button click and trigger the alert of the submission. 我的建议是重新设计代码,以在单击按钮时检查每个输入,并触发提交警报。 Instead of doing so that is overcomplicating things. 而不是这样做会使事情变得过于复杂。 So leave the button always visible and run the function on uploadButton click 因此,使该按钮始终可见,并在上载按钮上运行该函数,然后单击

Hope this will help you. 希望这会帮助你。 Initially Upload button is hidden when all the valid files are selected you will see upload button and any invalid type will give you alert. 最初,当所有有效文件都被选中时,“上载”按钮将被隐藏,您将看到“上载”按钮,任何无效的类型都会给您警报。

  var isValid = [0]; var sumTotal=0; function validate(fileNo){ var files = event.target.files; var filetype = files[0].type; if (filetype == 'image/jpeg' || filetype == 'image/jpeg' || filetype == 'image/png'){ isValid[fileNo]=1; }else{ alert("You must select a .jpg or .png, file."); isValid[fileNo]=0; } sumTotal = isValid.reduce(function(a, b) { return a + b; }, 0); if(sumTotal==5){ document.getElementById("uploadbutton").style.display="block"; }else{ document.getElementById("uploadbutton").style.display="none"; } } 
 <div id="upload"> <h2>If possible, please could you include photographs of the following:</h2> <p><label for='uploaded_file1'>Current boiler:</label> <input type="file" name="uploaded_file1" id="uploaded_file1" class="uploadfields" onChange="validate(1)"><a href="#" class="clearfile" id="clear1">X</a></p> <p><label for='uploaded_file2'>Gas meter:</label> <input type="file" name="uploaded_file2" id="uploaded_file2" class="uploadfields" onChange="validate(2)"><a href="#" class="clearfile" id="clear2">X</a></p> <p><label for='uploaded_file3'>Boiler pipe work:</label> <input type="file" name="uploaded_file3" id="uploaded_file3" class="uploadfields" onChange="validate(3)"><a href="#" class="clearfile" id="clear3">X</a></p> <p><label for='uploaded_file4'>Outside flue:</label> <input type="file" name="uploaded_file4" id="uploaded_file4" class="uploadfields" onChange="validate(4)"><a href="#" class="clearfile" id="clear4">X</a></p> <p><label for='uploaded_file5'>Anything else relevant:</label> <input type="file" name="uploaded_file5" id="uploaded_file5" class="uploadfields" onChange="validate(5)"><a href="#" class="clearfile" id="clear5">X</a></p><br /> <input class="backbutton showmoved" type="button" value="<< back" /> <input class="fwdbutton upload" style="display: none;" id="uploadbutton" type="button" value="upload >>" /> <p><a class="nophotos" id="nophotos">I have no photos &gt;&gt;</a></p> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM