JavaScript 多个错误消息
JavaScript Multiple Error Messages
用户输入,单击按钮,然后调用一个函数来执行一些计算。 如果字段的类型或值不正确,则会显示一条错误消息。 如果其中一个字段错误,则该字段需要显示错误消息。 如果多个字段错误,则需要显示错误消息。 现在,如果字段错误,即使所有字段都错误,页面也仅显示一条错误消息。 我确信这是一个简单的解决方案,但我总是用头撞墙。 有什么建议吗? 谢谢! 以下是代码的适用部分:
if (isNaN(theMilesDriven) || theMilesDriven <= 0) {
theMilesDrivenError.firstChild.nodeValue = "Please enter a number greater than 0 for the miles driven.";
return false;
}
if (isNaN(theGallonsUsed) || theGallonsUsed <= 0) {
theGallonsUsedError.firstChild.nodeValue = "Please enter a number greater than 0 for the gallons of gas used.";
return false;
}
if (isNaN(thePriceGallon) || thePriceGallon <= 0) {
thePriceGallonError.firstChild.nodeValue = "Please enter a number greater than 0 for the price per gallon.";
return false;
}
如果发现前两个错误之一,您的解决方案仍将进行计算。这是一个替代方案:
var isErrorFound = false;
if (isNaN(theMilesDriven) || theMilesDriven <= 0) {
theMilesDrivenError.firstChild.nodeValue = "Please enter a number greater than 0 for the miles driven.";
isErrorFound = true;
}
if (isNaN(theGallonsUsed) || theGallonsUsed <= 0) {
theGallonsUsedError.firstChild.nodeValue = "Please enter a number greater than 0 for the gallons of gas used.";
isErrorFound = true;
}
if (isNaN(thePriceGallon) || thePriceGallon <= 0) {
thePriceGallonError.firstChild.nodeValue = "Please enter a number greater than 0 for the price per gallon.";
isErrorFound = true;
}
if (!isErrorFound) {
//Perform the calculations and write the results to the page
var milesPerGallon = theMilesDriven / theGallonsUsed;
var theMPG = document.getElementById("mpg");
theMPG.firstChild.nodeValue = "Your MPG is: " + milesPerGallon.toFixed(2);
var cost = theGallonsUsed * thePriceGallon;
var theCost = document.getElementById("cost");
theCost.firstChild.nodeValue = "Your cost for this trip is: $" + cost.toFixed(2);
}
以下是我所做的似乎可以解决问题的方法:
//Verify the input is numerical and greater than 0
if (isNaN(theMilesDriven) || theMilesDriven <= 0) {
theMilesDrivenError.firstChild.nodeValue = "Please enter a number greater than 0 for the miles driven.";
}
if (isNaN(theGallonsUsed) || theGallonsUsed <= 0) {
theGallonsUsedError.firstChild.nodeValue = "Please enter a number greater than 0 for the gallons of gas used.";
}
if (isNaN(thePriceGallon) || thePriceGallon <= 0) {
thePriceGallonError.firstChild.nodeValue = "Please enter a number greater than 0 for the price per gallon.";
}
else {
//Perform the calculations and write the results to the page
var milesPerGallon = theMilesDriven / theGallonsUsed;
var theMPG = document.getElementById("mpg");
theMPG.firstChild.nodeValue = "Your MPG is: " + milesPerGallon.toFixed(2);
var cost = theGallonsUsed * thePriceGallon;
var theCost = document.getElementById("cost");
theCost.firstChild.nodeValue = "Your cost for this trip is: $" + cost.toFixed(2);
}
相关文章:
- jQuery在输入下验证post错误消息
- ng消息仅在触摸时显示错误,并在错误的初始显示上转换
- 解析云代码错误:解析.错误{代码:141,消息:“未调用成功/错误”}
- 在AngularJs中隐藏默认错误消息
- 如果用户输入的长度小于最小长度,则显示错误消息
- 使用ASP.NET CustomValidator避免重复的错误消息
- KOValidation在错误消息中获取可观察值、$index()、$data等
- 更改精细上载中的错误消息
- 如何在页面刷新时隐藏错误消息
- 如何显示错误消息
- 滑块未显示,控制台中没有错误消息
- 表单提交没有'如果为空,则不会显示错误消息
- 为什么我的Alexa技能测试显示正确的lambda输出,但在开发人员控制台中测试时却给出错误消息
- 隐藏错误消息“;未选择文件”;asp:fileUpload的文本
- 如何在消息中抛出tv4错误
- 登录时显示自定义错误消息
- jQuery AJAX 调用以发布消息 - 错误 堆栈空间不足
- 不断收到'向云PubSub发送测试消息错误…'Google Cloud PubSub
- 抑制WebBrowser JavaScript消息错误
- 在Chrome中解析SVG路径日志消息错误