简体   繁体   English

检查是否至少一个文本字段填充了jQuery

[英]Check if at least one textfield is filled with jQuery

I have a HTML form, which when submitted would send out a mail. 我有一个HTML表单,提交后会发送邮件。 I have completed the mail sending and everything else, however, I have a specific validation strategy. 我已经完成了邮件发送以及所有其他操作,但是,我有一个特定的验证策略。 Either the phone number or the email fields needs to be filled. 电话号码或电子邮件字段都需要填写。 Both are not mandatory fields, but using jQuery, I would like to make at least on of them mandatory. 两者都不是必填字段,但是我想使用jQuery至少使它们成为必填字段。 The form shall not submit without either phone or email. 表格必须在没有电话或电子邮件的情况下提交。 I am new to jQuery. 我是jQuery新手。 My form is as follows: 我的表格如下:

 <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>

Something like this should do it: 这样的事情应该做到:

//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.
});

First, you need to set a variable in which you'll store your result: 首先,您需要设置一个变量,用于存储结果:

var atLeastOneFilled = false;

Then, you need to go through all the fields you're interested in (in this example - #email, #Phone): 然后,您需要浏览所有感兴趣的字段(在此示例中-#email,#Phone):

$('#email, #Phone').each(function(index, field) { ... });

Then, we need to check if any of the fields is filled in, so in each() function (where I've placed '...') we can write, for example: 然后,我们需要检查是否填写了任何字段,因此在each()函数(我放置了“ ...”的位置)中,我们可以编写例如:

if($(field).val !== '')
    atLeastOneFilled = true;

This way, if at least one field would have a value different from "" (nothing), our flag atLeastOneFilled would be changed to true. 这样,如果至少一个字段的值与“”(无)不同,则我们的atLeastOneFilled标志将更改为true。

Then, you can do whatever you want with your variable: 然后,您可以使用变量执行任何操作:

if(atLeastOneFilled) {
    doSomething();
}

Below condition will check anyone of the value should be true 以下条件将检查任何人的值应为true

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

You can use a filter : 您可以使用filter

$(function () {
  $("form").submit(function () {
    if ($("input").filter(function () {
      return $(this).val().trim().length > 0;
    }).length == 0)
      return false;
  });
});
<script>
$( "#form" ).validate({
  rules: {
    phone: {
      require_from_group: [1, ".form-group"]
    },
    email: {
      require_from_group: [1, ".form-group"]
    }
  }
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM