Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 需要帮助验证单选按钮吗_Javascript_Forms_Radio Button_Validation - Fatal编程技术网

Javascript 需要帮助验证单选按钮吗

Javascript 需要帮助验证单选按钮吗,javascript,forms,radio-button,validation,Javascript,Forms,Radio Button,Validation,试图让我的最终项目按时准备好(这一次),但我似乎无法让单选按钮组在提交表单时进行验证。我有其他一切工作,验证是如此,然而,无线电组是顽固的。我尝试过不同的方法,例如删除变量并直接指向“group1”,但没有任何效果。任何帮助都将不胜感激 <script type="text/javascript"> function ValidateContactForm() { var name = document.ContactForm.Name; va

试图让我的最终项目按时准备好(这一次),但我似乎无法让单选按钮组在提交表单时进行验证。我有其他一切工作,验证是如此,然而,无线电组是顽固的。我尝试过不同的方法,例如删除变量并直接指向“group1”,但没有任何效果。任何帮助都将不胜感激

    <script type="text/javascript">
    function ValidateContactForm()
    {
    var name = document.ContactForm.Name;
    var email = document.ContactForm.Email;
    var phone = document.ContactForm.areaCode;
    var what = document.ContactForm.Subject;
    var comment = document.ContactForm.Comment;
    var btn = document.ContactForm.group1;

    if (name.value == "")
    {
        window.alert("Please enter your name.");
        name.focus();
        return false;
    }

    if (email.value == "")
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf("@", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf(".", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }

    if  (btn.value > 0) {
        window.alert("Please choose method of contact.");
        btn.focus();
        return false;
        }

    if (phone.value == "")
    {
        window.alert("Please enter your telephone number.");
        phone.focus();
        return false;
    }

    if (what.selectedIndex < 1)
    {
        alert("Please tell us how we can help you.");
        what.focus();
        return false;
    }

    if (comment.value == "")
    {
        window.alert("Please provide a detailed description or comment.");
        comment.focus();
    return false;
            }
    }
    function validateFixedLengthNumericField(numericField, len){

    if (len != numericField.value.length){
        numericField.focus();   
        alert('Field must contain exactly '+len+' numbers.');
    }
    }

    function validateNextField(numericField, len, nextField){

    if (len == numericField.value.length){
        nextField.focus();
    }
    }
    </script>

    <form method="post" onSubmit="return ValidateContactForm();" name="ContactForm">
  <div align="center">
      <table border="2">
    <tr>
      <td valign="top"><h3 align="left">Contact Information</h3>
    <p align="left">Name:<br /> <input type="text" size="65" name="Name"></p>
    <p align="left">E-mail Address:<br /><input type="text" size="65" name="Email"></p>
    <p align="left">Telephone:<br /> 
      <div class="Telephone" style="float:left;">
            (<input name="areaCode" id="areaCode"  type="text"  maxlength="3"
                onkeyup="validateNextField(this,3,phonePrefix);" 
                onblur="validateFixedLengthNumericField(this,3);"  
                style="font-size:11px; width:20px;"  title="Area Code" autocomplete="off">) 
            <input name="phonePrefix" id="phonePrefix" type="text" 
                onkeyup="validateNextField(this,3,phoneSuffix);" 
                onblur="validateFixedLengthNumericField(this,3);"  
                style="font-size:11px; width:20px;" maxlength="3"  title="Phone Prefix"  autocomplete="off">-
            <input name="phoneSuffix" id="phoneSuffix" type="text"  maxlength="4" 
                onkeyup="validateNextField(this,3,phoneExtension);" 
                onblur="validateFixedLengthNumericField(this,4);"  
                style="font-size:11px; width:25px;" title="Phone Suffix"  autocomplete="off"></p>
    </div>
      <p align="left">&nbsp;</p>
      <p align="left"><strong>Would you like to sign up for one of our mailings?</strong>  <br />
      </p>
    <div align="left">
      <input type="checkbox" name="option" value="newsletter" />
      Newsletters<br />
      <input type="checkbox" name="option" value="events" />
      Events<br />
      <input type="checkbox" name="option" value="grando" />
      Grand Openings<br />
      <input type="checkbox" name="option" value="coupon" />
      Coupons<br />
      <input type="checkbox" name="option" value="other" />
      Other</div>
    <p align="left"><strong>How do you perfer us to contact you</strong><br />
    </p>
    <div align="left">
      <input type="radio" name="group1" id="r1" value="1" />
      Phone
      <br />
    <input type="radio" name="group1" id="r2" value="2" />
    Email<br />
    <input type="radio" name="group1" id="r3" value="3" />
    Snail Mail
    </div>
    <p align="left">What can we help you with?
      <select type="text" value="" name="Subject">
            <option>  </option>
            <option>Customer Service</option>
            <option>Question</option>
            <option>Comment</option>
            <option>Complaint</option>
            <option>Other</option>
    </select></p>
    <p align="left">Comments:</p>
    <p align="center">
      <textarea cols="55" rows="10" name="Comment"></textarea>
    </p>
    <p align="center"><input type="submit" value="Send" name="submit"><input type="reset"      value="Reset" name="reset">    
      </form>

函数ValidateContactForm()
{
var name=document.ContactForm.name;
var email=document.ContactForm.email;
var phone=document.ContactForm.areaCode;
var what=document.ContactForm.Subject;
var comment=document.ContactForm.comment;
var btn=document.ContactForm.group1;
如果(name.value==“”)
{
window.alert(“请输入您的姓名”);
name.focus();
返回false;
}
如果(email.value==“”)
{
window.alert(“请输入有效的电子邮件地址”);
email.focus();
返回false;
}
if(email.value.indexOf(“@”,0)<0)
{
window.alert(“请输入有效的电子邮件地址”);
email.focus();
返回false;
}
if(email.value.indexOf(“.”,0)<0)
{
window.alert(“请输入有效的电子邮件地址”);
email.focus();
返回false;
}
如果(btn.value>0){
window.alert(“请选择联系方式”);
btn.focus();
返回false;
}
如果(phone.value==“”)
{
窗口提示(“请输入您的电话号码”);
phone.focus();
返回false;
}
if(what.selectedIndex<1)
{
警惕(“请告诉我们如何帮助您。”);
什么。焦点();
返回false;
}
如果(comment.value==“”)
{
警告(“请提供详细说明或评论”);
comment.focus();
返回false;
}
}
函数validateExedLengthNumericField(numericField,len){
if(len!=numericField.value.length){
numericField.focus();
警报('字段必须精确包含'+len+'数字');
}
}
函数validateNextField(数值字段、len、nextField){
if(len==numericField.value.length){
nextField.focus();
}
}
联系方式

名称:

电子邮件地址:

电话:
() -

您想注册我们的邮件吗?

时事通讯
事件
盛大开幕式
优惠券
其他

您希望我们如何与您联系

电话
电子邮件
平信

我们能为您提供哪些帮助? 客户服务 问题: 评论 抱怨 其他

评论:


在本例中,变量btn包含一组单选按钮。因此,您需要循环遍历它,并找到哪一个被选中。比如:

var somethingChecked = false;
for (i = 0; i < btn.length; i++) {
    if (btn[i].checked) {
        somethingChecked = true;
    }
}
if (!somethingChecked) {
    window.alert("Please choose method of contact.");
    return false;
}
var somethingChecked=false;
对于(i=0;i
在本例中,变量btn包含一组单选按钮。因此,您需要循环遍历它,并找到哪一个被选中。比如:

var somethingChecked = false;
for (i = 0; i < btn.length; i++) {
    if (btn[i].checked) {
        somethingChecked = true;
    }
}
if (!somethingChecked) {
    window.alert("Please choose method of contact.");
    return false;
}
var somethingChecked=false;
对于(i=0;i
使用一些正则表达式进行电子邮件验证;)让一些正则表达式用于电子邮件验证;)工作得很有魅力。。。非常感谢。我所做的唯一改动是删除了以下代码后的第二个括号:somethingChecked=true;我更正了代码。“for”之后缺少一个开头括号。你能把这个作为你问题的答案吗?工作很有魅力。。。非常感谢。我所做的唯一改动是删除了以下代码后的第二个括号:somethingChecked=true;我更正了代码。“for”之后缺少一个开头括号。你能把这个作为你问题的答案吗?