使用javascript验证表单值
Validating form values with javascript
我有一个要用javascript验证的表单。我的表单有两个单选按钮,询问用户是否同意某个分类,如果不同意,他可以从下拉列表中为自己选择一个新的分类。在javascript中,我想过滤掉无效的输入组合。
<html>
<head>
<script type="text/javascript">
function isValidForm()
{
if(document.validateClassificationForm.acceptclassification.value == 'yes' && document.validateClassificationForm.newclassification.value != 'default')
{
if (confirm('You chose the option to accept the classification obtained by the classifier, but you chose another classification from the drop-down list. Do you want to override the classification by the one you chose?'))
return true;
else
return false;
}
if (document.validateClassificationForm.acceptclassification.value == 'no' && document.validateClassificationForm.newclassification.value == 'default')
{
alert('You chose the option not to accept the classification obtained by the classifier. Please chose another classification from the drop-down list.');
return false;
}
return true;
}
</script>
</head>
<body>
<form method=post onsubmit="return isValidForm()" action="index.php?operacao=validateClassification" name="validateClassificationForm">
<ol>
<li><p>Do you agree with the classification attributed to this patient?</p></li>
<li>Yes<input type="radio" name="acceptclassification" value="yes" checked><br></li>
<li>No<input type="radio" name="acceptclassification" value="no"></li>
<li><p>If you don't agree, chose another classification:</p></li>
<li><label for="newclassification">Chose classification:</label>
<select name="newclassification">
<option value="default">Select</option>
<option value="emergency">Emergency</option>
<option value="veryurgent">Very urgent</option>
<option value="urgent">Urgent</option>
<option value="slightlyurgent">Slightly urgent</option>
<option value="noturgent">Not urgent</option>
</select></li>
<div id="form-button">
<li><input type="submit" value="Submit"></li>
</div>
</ol>
</form>
</body>
</html>
尽管如此,javascript似乎从未被执行过,表单总是被提交的。我在这里做错了什么?
document.validateClassificationForm.acceptclassification
返回nodeList对象。因此,不能简单地编写document.validateClassificationForm.acceptclassification.value
nodeList对象没有名为值的属性。
function isValidForm() {
var acceptclassification = Array.prototype.slice.call(document.validateClassificationForm.acceptclassification).filter(function (element) {
return element.checked;
})[0];
if (acceptclassification.value == 'yes' && document.validateClassificationForm.newclassification.value != 'default') {
if (confirm('You chose the option to accept the classification obtained by the classifier, but you chose another classification from the drop-down list. Do you want to override the classification by the one you chose?')) return true;
else return false;
}
if (acceptclassification.value == 'no' && document.validateClassificationForm.newclassification.value == 'default') {
alert('You chose the option not to accept the classification obtained by the classifier. Please chose another classification from the drop-down list.');
return false;
}
return true;
}
打开它http://jsfiddle.net/4n8S6/
相关文章:
- 解析javascript表单验证器
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 自定义表单验证和提交
- Javascript表单验证的缺点
- Jquery的表单验证不起作用
- 语义ui表单验证:图像url
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- JavaScript表单验证-“;“错误”;不按需要工作
- Mailchimp内联表单验证不适用于JS加载的HTML
- jQuery/JS获胜't在表单验证后重定向
- jquery表单验证复选框错误显示
- javascript表单验证-数值和密码
- 我想知道如何在javascript中进行表单验证
- jQuery表单验证(如果==“执行此操作”)仅适用于第一次输入
- 使用记录保存的消息进行表单验证
- 表单验证工作不正常,在不检查条目的情况下继续
- 如何对未知表单元素进行表单验证
- 使用ajax、php和mysql进行表单验证
- JS表单验证未发生
- 表单验证jsphp路径