JavaScript 简单的“错误处理”通过用条件替换 NaN
我想知道如何检测错误的输入类型,而不是向用户呈现 NaN,而是显示一条逻辑信息。
这是我的代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script>
window.onload = function() {
document.getElementById('calculate').onclick = function calculateQuad()
{
var inputa = document.getElementById('variablea').value;
var inputb = document.getElementById('variableb').value;
var inputc = document.getElementById('variablec').value;
root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/2*inputa
root2 = (-inputb + Math.sqrt(root))/2*inputa
document.getElementById('root1').value = root1;
document.getElementById('root2').value = root2;
if(root<'0')
{
document.getElementById('root1').value = 'No real solution'
document.getElementById('root2').value = 'No real solution'
}
else {
if(root=='0')
{
document.getElementById('root1').value = root1
document.getElementById('root2').value = 'No Second Answer'
}
else {
document.getElementById('root1').value = root1
document.getElementById('root2').value = root1
}
}
};
document.getElementById('erase').onclick = function()
{
document.getElementById('form1').reset();
}
}
</script>
<style>
#container
{
text-align: center;
}
</style>
</head>
<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
a:<input id="variablea" value="" type="text">
<br/>
b:<input id="variableb" value="" type="text">
<br />
c:<input id="variablec" value="" type="text">
<br />
<input id="calculate" value="Calculate!" type="button">
<input id="erase" value="Clear" type="button">
<br />
<br />
Roots:
<br />
<input id="root1" type="text" readonly>
<br />
<input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>
所以,假设您在 b
输入字段中输入 10a,但是 a = 1
和 c = 0
我会希望能够检测到不正确的输入,并打印“请仅输入整数”或类似的内容。
I was wondering how I can detect false input type and instead of presenting the user with NaN, have a logical piece of information be displayed.
Here is my code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script>
window.onload = function() {
document.getElementById('calculate').onclick = function calculateQuad()
{
var inputa = document.getElementById('variablea').value;
var inputb = document.getElementById('variableb').value;
var inputc = document.getElementById('variablec').value;
root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/2*inputa
root2 = (-inputb + Math.sqrt(root))/2*inputa
document.getElementById('root1').value = root1;
document.getElementById('root2').value = root2;
if(root<'0')
{
document.getElementById('root1').value = 'No real solution'
document.getElementById('root2').value = 'No real solution'
}
else {
if(root=='0')
{
document.getElementById('root1').value = root1
document.getElementById('root2').value = 'No Second Answer'
}
else {
document.getElementById('root1').value = root1
document.getElementById('root2').value = root1
}
}
};
document.getElementById('erase').onclick = function()
{
document.getElementById('form1').reset();
}
}
</script>
<style>
#container
{
text-align: center;
}
</style>
</head>
<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
a:<input id="variablea" value="" type="text">
<br/>
b:<input id="variableb" value="" type="text">
<br />
c:<input id="variablec" value="" type="text">
<br />
<input id="calculate" value="Calculate!" type="button">
<input id="erase" value="Clear" type="button">
<br />
<br />
Roots:
<br />
<input id="root1" type="text" readonly>
<br />
<input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>
So, say for instance you type in 10a in the b
input field but a = 1
and c = 0
I would like to be able to detect the incorrect input, and print "Please only input integers" or something along those lines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很简单,这是一个完整的示例,输出漂亮的错误消息;)
It's rather simple, Here is a complete sample, outputting beautiful error messages to ;)
您可以在尝试在计算中使用输入之前检查输入。
You can check the input before you try to use it in a calculation.
我认为你需要这样的东西
并这样称呼它:
NaNValueReplacement('txt_field_1','txt_field_2','txt_field_3','txt_field_4');
您可以根据需要放置任意数量的 txt_field。
我还在这里放置了一个示例 http://jsfiddle.net/lidakis/mveWy/6/
希望有帮助。
I think that you need something like this
And Call it like this:
NaNValueReplacement('txt_field_1','txt_field_2','txt_field_3','txt_field_4');
You can place as many txt_fields as you want.
Also I have placed an example here http://jsfiddle.net/lidakis/mveWy/6/
Hope It Helps.