JavaScript输入类型验证
我似乎无法找到正确的JavaScript来验证此表单。请帮助/提供反馈 基本上,我的脚本应该验证用户是否在输入文本框中输入了数据、是否选中了单选按钮、是否至少选中了一个复选框以及是否从选择项中选择了一个选项 此外,表单还使用提交按钮来调用验证脚本,以便仅当表单字段被验证和接受时才处理表单。如果字段无效,则向用户显示消息 还需要确保表单不会在用户每次收到验证错误时自动重置JavaScript输入类型验证,javascript,forms,validation,Javascript,Forms,Validation,我似乎无法找到正确的JavaScript来验证此表单。请帮助/提供反馈 基本上,我的脚本应该验证用户是否在输入文本框中输入了数据、是否选中了单选按钮、是否至少选中了一个复选框以及是否从选择项中选择了一个选项 此外,表单还使用提交按钮来调用验证脚本,以便仅当表单字段被验证和接受时才处理表单。如果字段无效,则向用户显示消息 还需要确保表单不会在用户每次收到验证错误时自动重置 <body> <section> <h1 style="text-align:
<body>
<section>
<h1 style="text-align: center">Vacation Interest Vote Form</h1>
<form name="VacayForm" action="mailto:" onsubmit="return Validate1()" method="post">
<p>Name:<input type="text" name="name" size="25"></p><br>
<p>Do You Prefer an international destination?</p>
<p>Domestic<input type="radio" name="domint" value="domestic"></p>
<p>International<input type="radio" name="domint" value="international"></
<br>
<p>Where would you like to go?</p>
<select type="text" name="continent" value="select" size="1">
<option value="domestic">Domestic</option>
<option value="europe">Europe</option>
<option value="camerica">Central America</option>
<option value="asia">Asia</option>
<option value="aus">Australia</option>
</select>
<br>
<p>Check the box to act as your digital signature to cast your vote
<input type="checkbox" value="agree" name="sig">
<input type="submit" value="Send" name="submit" onclick="if(!this.form.sig.checked){alert('You must agree to cast your vote by checking the box.');
return false}">
<input type="reset" value="Reset"name="reset">
</form>
</section>
<script>
function Validate1() {
var nam = document.forms["VacayForm"]["name"];
var dom = document.forms["VacayForm"]["domestic"];
var int = document.forms["VacayForm"]["international"];
var sel = document.forms["VacayForm"]["select"];
var agree = document.forms["VacayForm"]["agree"];
//if (name.value == "")
//{
// window.alert("Please enter your name.");
// name.focus();
// return false;
//}
if( document.VacayForm.name.value == "" )
{
alert( "Please provide your name!" );
document.VacayForm.name.focus() ;
return false;
}
if (domestic.value == "")
else (international.value == "")
{
window.alert("Please select domestic or international preference to proceed.");
domestic.focus();
international.focus();
return false;
}
if (select.selectedIndex < 1)
{
alert("Please select where you prefer to visit");
select.focus();
return false;
}
return true;
}
//function Validate2() {
// var radios = document.getElementsByName("yesno");
// var formValid = false;
// var i = 0;
// while (!formValid && i < radios.length) {
// if (radios[i].checked) formValid = true;
// i++;
// }
// if (!formValid) alert("Must check an option!");
// return formValid;
//}
</script>
</body>
</html>
假期利息投票表格
姓名:
你喜欢国际目的地吗
国内的
国际您的验证函数可能如下所示
function validate() {
var form = document.forms.VacayForm;
var name = form.name;
var domInt = form.domint;
var continent = form.continent;
var agree = form.agree;
if (!name.value) {
alert( "Please provide your name!" );
name.focus();
return false;
}
if (!domInt.value) {
alert( "Please select domestic or international preference to proceed" );
domInt.focus();
return false;
}
if (!continent.value) {
alert("Please select where you prefer to visit");
continent.focus();
return false;
}
if (!agree.checked) {
alert("Please check agree to continue");
agree.focus();
return false;
}
return true;
}
从Javascript.info网站学习如何使用表单和表单元素…当问题与jQuery验证插件完全无关时,请不要使用jQuery验证标签。我也可以使用Jquery验证,只要有JS。所以我用它。。。我真希望你在要求我亲自编辑我的帖子之前,不要自找麻烦。。标签用于根据内容对问题进行分类,任何有权编辑帖子的人都可以根据需要进行编辑。因为问题不是询问jQuery验证插件,也不包含任何与jQuery验证插件相关的代码,“jQuery验证”标签在这里绝对不合适。谢谢。非常感谢你的文章参考。我将阅读文档并尝试验证!!不幸的是,验证功能不起作用:(