使用 jQuery 验证编写条件规则

发布于 2024-07-29 07:05:21 字数 1699 浏览 4 评论 0原文

您好,我有一个简单的模式弹出窗口,其中有 3 个名称字段和 3 个彼此相邻的电子邮件字段。 我是 jQuery 新手,所以有人可以帮助我如何为以下情况编写逻辑吗?

名字字段和电子邮件字段是必填字段,因此我将类保留为必填字段,但其他两个姓名和电子邮件字段是可选的,但如果我在姓名字段中输入值,则必须在电子邮件字段中输入值。 换句话说,如果姓名字段不为空,则电子邮件字段不应为空。 请告诉我是否有人可以帮助我,谢谢。

<div class="leftCol">
        <label for="emailToName1" class="formLabel"><span class="required">*</span>Name:</label>
          <div><input type="text" id="emailToName1" name="referName01" value="${referAFriendBean.referName01!}" class="required" tabindex="1" /></div>
          <div class="pad5Top"><input type="text" id="emailToName2" name="referName02" value="${referAFriendBean.referName02!}" tabindex="3" /></div>
          <div class="pad5Top"><input type="text" id="emailToName3" name="referName03" value="${referAFriendBean.referName03!}" tabindex="5" /></div>
        </div>

        <div class="rightCol">
            <label for="emailToAddress1" class="formLabel"><span class="required">*</span>Email address:</label>
          <div><input type="text" id="emailToAddress1" name="referEmail01" value="${referAFriendBean.referEmail01!}" class="required email" tabindex="2" /></div>
          <div class="pad5Top"><input type="text" id="emailToAddress2" name="referEmail02" class="email" tabindex="4" value="${referAFriendBean.referEmail02!}" /></div>
          <div class="pad5Top"><input type="text" id="emailToAddress3" name="referEmail03" class="email" tabindex="6" value="${referAFriendBean.referEmail03!}" /></div>
        </div>
      </div>

Hi I have a simple modal popup which has 3 name fields and 3 e mail fields adjacent to each other. I am new to jQuery so can any one please help me how to write a logic for the following case?

The first name field and Email field are mandatory so I kept the class as required fields but the other two name and e-mail fields are optional but if I enter a value in name field I have to enter the value in Email field. In other words if name field is not empty email field should not be empty. Please let me know if some one can help me thank you.

<div class="leftCol">
        <label for="emailToName1" class="formLabel"><span class="required">*</span>Name:</label>
          <div><input type="text" id="emailToName1" name="referName01" value="${referAFriendBean.referName01!}" class="required" tabindex="1" /></div>
          <div class="pad5Top"><input type="text" id="emailToName2" name="referName02" value="${referAFriendBean.referName02!}" tabindex="3" /></div>
          <div class="pad5Top"><input type="text" id="emailToName3" name="referName03" value="${referAFriendBean.referName03!}" tabindex="5" /></div>
        </div>

        <div class="rightCol">
            <label for="emailToAddress1" class="formLabel"><span class="required">*</span>Email address:</label>
          <div><input type="text" id="emailToAddress1" name="referEmail01" value="${referAFriendBean.referEmail01!}" class="required email" tabindex="2" /></div>
          <div class="pad5Top"><input type="text" id="emailToAddress2" name="referEmail02" class="email" tabindex="4" value="${referAFriendBean.referEmail02!}" /></div>
          <div class="pad5Top"><input type="text" id="emailToAddress3" name="referEmail03" class="email" tabindex="6" value="${referAFriendBean.referEmail03!}" /></div>
        </div>
      </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴随着你 2024-08-05 07:05:21

也许是这样的:

if($('emailToName2').val() != '' && $('emailToAddress2').val() == ''){
//do something here
}

您还可以检查每个值的长度。

if($('emailToName2').val().legnth > 0 && $('emailToAddress2').val().length == 0){
//do something here
}

Perhaps something like this:

if($('emailToName2').val() != '' && $('emailToAddress2').val() == ''){
//do something here
}

You could also check the length of each value.

if($('emailToName2').val().legnth > 0 && $('emailToAddress2').val().length == 0){
//do something here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文