【问题标题】:Javascript Email Id Validation with checkbox带有复选框的 Javascript 电子邮件 ID 验证
【发布时间】:2020-04-09 18:46:33
【问题描述】:

我希望进行 Javascript 电子邮件 ID 和复选框验证。验证仅适用于复选框,但不适用于电子邮件 ID。请问如何改正?

codepen demo

function Validate()  
{  
    var x=document.myform.email.value;  
    var atposition=x.indexOf("@");  
    var dotposition=x.lastIndexOf(".");  
    if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){  
        alert("Please enter a valid e-mail address");  
        return false;  
    }  
    return true;
}  


function Validate(){
   if(!validateForm()){
       alert("Terms & Conditions!");
       return false;
   }
   return true
}

function validateForm()
{
    var c=document.getElementsByTagName('input');
    for (var i = 0; i<c.length; i++){
        if (c[i].type=='checkbox')
        {
            if (c[i].checked){return true}
        }
    }
    return false;
}

【问题讨论】:

    标签: javascript jquery validation


    【解决方案1】:

    	// this function is called on form submit and checks the return values of both the email and checkbox function. if either of them are false, you will be alerted.
      function MainFunction(){
       if(!validateCheckBox() || !ValidateEmail()  ){
           return false;
       }
         alert("the form has been successfully submitted");
        return true
        }
    
    // this function validates the email
    function ValidateEmail()  
    {  
    var x=document.myform.email.value;  
    var atposition=x.indexOf("@");  
    var dotposition=x.lastIndexOf(".");  
    if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){  
      alert("Please enter a valid e-mail address");  
      return false;  
      }  
      return true;
    }  
    
    // this function validates the checkbox
    function validateCheckBox()
    {
        var c=document.getElementsByTagName('input');
        for (var i = 0; i<c.length; i++){
            if (c[i].type=='checkbox')
            {
                if (c[i].checked){return true}
            }
        }
         alert("Terms & Conditions!");
        return false;
    }
    //you had two of the same named function before. also you were only checking the return of one of the functions. Above checks the return values of both of the functions. Hope this helps
    <form name="myform" id="form_id"  method="post" onsubmit="return MainFunction();">
    <input type="text" name="email" class="subscribe_email_nf" placeholder="Enter Your Email Id...">	<br />
    <input type="checkbox" name="option1" value="1">Accept Terms & Conditions<br />
    <input type="submit" value="Submit Form">
    </form>

    【讨论】:

    • 谢谢,亚历克斯。真的很欣赏它。有没有办法发出关于“输入电子邮件”而不是“条款和条件!”的第一个警报。目前,第一个警报是针对复选框(条款和条件!)。
    • 哦!是的...现在工作正常。当我将语法重新排序为 - !ValidateEmail(),!ValidateForm() THANKS ALEX.
    • 没问题!此外,如果您想检查缺少哪些值,请在主函数内分别为每个函数添加 if 语句。
    • 非常感谢,亚历克斯。真的很欣赏它。 :)
    猜你喜欢
    • 2019-02-17
    • 2010-10-19
    • 1970-01-01
    • 2023-04-06
    • 2011-10-20
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多