Javascript 检查是否至少有一个文本字段用jQuery填充

Javascript 检查是否至少有一个文本字段用jQuery填充,javascript,jquery,validation,Javascript,Jquery,Validation,我有一个HTML表单,提交后会发送邮件。我已经完成了邮件发送和其他一切,但是,我有一个特定的验证策略。需要填写电话号码或电子邮件字段。这两个字段都不是必填字段,但使用jQuery,我想至少对其中的一个字段进行必填。未经电话或电子邮件,不得提交表格。我不熟悉jQuery。我的表格如下: <form name="myform" id="myform" role="form" method="post" action="mail.php"> <div class="form

我有一个HTML表单,提交后会发送邮件。我已经完成了邮件发送和其他一切,但是,我有一个特定的验证策略。需要填写电话号码或电子邮件字段。这两个字段都不是必填字段,但使用jQuery,我想至少对其中的一个字段进行必填。未经电话或电子邮件,不得提交表格。我不熟悉jQuery。我的表格如下:

 <form name="myform" id="myform" role="form" method="post" action="mail.php">
    <div class="form-group">
        <label for="Phone">Phone*:</label>
        <input type="type" class="form-control" id="Phone" placeholder="Enter Phone">
        <label for="fn">First Name*:</label>
        <input type="type" class="form-control" id="fn" placeholder="Enter First Name" required>
    </div>
    <div class="form-group">
        <label for="fn">Surname:</label>
        <input type="type" class="form-control" id="fn" placeholder="Enter Surname">
        <label for="email">Email:</label>
        <input type="email" class="form-control" id="email" placeholder="Enter email">
    </div>
    <div class="form-group">
        <button type="submit" class="btn submit pull-right">Submit</button>
    </div>
</form>

电话*:
姓名*:
姓:
电邮:
提交

以下条件将检查任何值是否为真

if($('#email').val() || $('#Phone').val()){
   //do your actions here.
}

下面的条件将检查任何值是否为真

if($('#email').val() || $('#Phone').val()){
   //do your actions here.
}

首先,您需要设置一个用于存储结果的变量:

var atLeastOneFilled = false;
然后,您需要浏览所有感兴趣的领域(在本例中为电子邮件、电话):

然后,我们需要检查是否填写了任何字段,因此在每个()函数中(我在其中放置了“…”),我们可以编写,例如:

if($(field).val !== '')
    atLeastOneFilled = true;
这样,如果至少有一个字段的值与“”(nothing)不同,则我们的标志atLeastOneFilled将更改为true

然后,可以对变量执行任何操作:

if(atLeastOneFilled) {
    doSomething();
}

首先,您需要设置一个用于存储结果的变量:

var atLeastOneFilled = false;
然后,您需要浏览所有感兴趣的领域(在本例中为电子邮件、电话):

然后,我们需要检查是否填写了任何字段,因此在每个()函数中(我在其中放置了“…”),我们可以编写,例如:

if($(field).val !== '')
    atLeastOneFilled = true;
这样,如果至少有一个字段的值与“”(nothing)不同,则我们的标志atLeastOneFilled将更改为true

然后,可以对变量执行任何操作:

if(atLeastOneFilled) {
    doSomething();
}

您可以使用
过滤器

$(function () {
  $("form").submit(function () {
    if ($("input").filter(function () {
      return $(this).val().trim().length > 0;
    }).length == 0)
      return false;
  });
});

您可以使用
过滤器

$(function () {
  $("form").submit(function () {
    if ($("input").filter(function () {
      return $(this).val().trim().length > 0;
    }).length == 0)
      return false;
  });
});

$(“#表格”)。验证({
规则:{
电话:{
需要来自组的组:[1,“.form group”]
},
电邮:{
需要来自组的组:[1,“.form group”]
}
}
});

$(“#表格”)。验证({
规则:{
电话:{
需要来自组的组:[1,“.form group”]
},
电邮:{
需要来自组的组:[1,“.form group”]
}
}
});

像这样的东西应该可以做到:

//When the form is submitted...
//Might want to give it an ID so you can bind to that instead (i.e. #myform)
$("form").submit(function() {
    //If both of them are empty...
    if($("#email").val().length === 0 && $("#Phone").val().length === 0) {
        //Notify the user.
        alert("You need to enter an email or a phone number.");
        //And do not submit the form.
        event.preventDefault();
    }
    //Here you can do other stuff related to submitting the form.
});

像这样的东西应该可以做到:

//When the form is submitted...
//Might want to give it an ID so you can bind to that instead (i.e. #myform)
$("form").submit(function() {
    //If both of them are empty...
    if($("#email").val().length === 0 && $("#Phone").val().length === 0) {
        //Notify the user.
        alert("You need to enter an email or a phone number.");
        //And do not submit the form.
        event.preventDefault();
    }
    //Here you can do other stuff related to submitting the form.
});

这不是本机jQuery功能;你需要一个jQuery插件来实现这一点;你需要一个jQuery插件。我是jQuery新手。你能给我提供更多的代码吗?我是jQuery新手。你能给我提供更多的代码吗?