简体   繁体   English

我可以使用这个 JavaScript 验证两个字段而不是一个字段吗?

[英]Can I Validate Two Fields Instead of One Field With This JavaScript?

I use the javascript below to validate the Phone Number field on my form by showing the next hidden field whenever the value the user inputs in the phone number field on my form matches with the values in my javascript.只要用户在表单上的电话号码字段中输入的值与我的 javascript 中的值匹配,我就会使用下面的 javascript 通过显示下一个隐藏字段来验证表单上的电话号码字段。

however, if their input does not match with the values in my javascript, the hidden field remains hidden and the user will be unable to submit the form.但是,如果他们的输入与我的 javascript 中的值不匹配,则隐藏字段将保持隐藏状态,用户将无法提交表单。

The javascript works fine for the phone field but I am trying to validate two different fields to match with the values in my javascript. javascript 适用于电话字段,但我正在尝试验证两个不同的字段以匹配我的 javascript 中的值。

For example, I want to make the values users input on the phone field and the email field of my form match with the values in my javascript before the hidden field shows.例如,我想让用户在电话字段和我的表单的电子邮件字段中输入的值在隐藏字段显示之前与我的 javascript 中的值匹配。

Illustration below;下图;

Lets say, the values in my javascript are;可以说,我的javascript中的值是; if (phone === "12345" && email === "12345@gmail.com")如果(电话===“12345”&&电子邮件===“12345@gmail.com”)

If the user inputs Phone: 12345 and their email: 12345@gmail.com, the hidden field shows .如果用户输入电话:12345 和他们的电子邮件:12345@gmail.com,隐藏字段显示

If the user inputs Phone:123 and their email: 12345@gmail.com, the hidden field remains hidden .如果用户输入电话:123 和他们的电子邮件:12345@gmail.com,隐藏字段保持隐藏

I have tried different solutions to validate the phone and email field but all my solutions failed and I need some help with this.我尝试了不同的解决方案来验证电话和电子邮件字段,但我所有的解决方案都失败了,我需要一些帮助。

Sorry if my solution below is poor but I am not the owner of the original code.抱歉,如果我下面的解决方案很差,但我不是原始代码的所有者。

Thanks for your help.谢谢你的帮助。

Below is my sample code for the phone field validation.下面是我的电话字段验证示例代码。 (WORKING FINE!) (工作正常!)

 $('.validate').hide(); $('body').on('blur', '#phone', function() { var value = $(this).val(); if (isPhoneInUse(value)) { $(".validate").show(); } else { alert ("Phone do not match!\nYou cannot submit this form!"); $(".validate").hide(); } }); $('#submitForm').on('submit', function(e) { var value = $("#phone").val(); if (isPhoneInUse(value)) { // validation failed. cancel the event console.log("not submitting"); return false; } }) function isPhoneInUse(phone) { return (phone === "1234" || phone === "23456") }
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <form action='' method='POST' id="submitForm"> <div class="validate"><span style="color: red;"><b>Phone Matches!</b></span></div> <input type="phone" name='phone' required='' id="phone" placeholder="0000-000-0000" /> <br/><br/> <div class="validate"> <button href='/' type='submit' id="submitForm">Submit</button> </div> </form>

Below is my solution to validate the phone and email fields.以下是我验证电话和电子邮件字段的解决方案。 (NOT WORKING!) (不工作!)

 $('.validate').hide(); $('body').on('blur', '#phone', '#email', function() { var value = $(this).val(); if (isDataInUse( $("#phone").val(), $("#email").val() )) { $(".validate").show(); } else { alert ("Phone and Email do not match!\nYou cannot submit this form!"); $(".validate").hide(); } }); $('#submitForm').on('submit', function(e) { var value = $("#phone" && "#email".val()); if (isDataInUse( $("#phone").val(), $("#email").val() )) { // validation failed. cancel the event console.log("not submitting"); event.preventDefault(); } }) function isDataInUse(phone, email) { return (phone === "1234" && email === "1234@gmail.com") }

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

<form action='' method='POST' id="submitForm">
<div class="validate"><span style="color: red;"><b>Phone and Email Matches!</b></span></div>

  <input type="phone" name='phone' required='' id="phone" placeholder="0000-000-0000" />
  <br/><br/>
    <input type="email" name='email' required='' id="email" placeholder="hello@youremail.com" />

  <br/><br/>
<div class="validate">
  <button href='/' type='submit' id="submitForm">Submit</button>
</div>
</form>

After many corrections, here is a working snippet:经过多次更正,这是一个工作片段:

 $('.validate').hide(); $('#phone, #email').on('change', function() { let phone = $('#phone').val(); let email = $('#email').val(); if (isDataInUse(phone, email)) { $(".validate").show(); } else { alert ("Phone or Email do not match!\nYou cannot submit this form!"); $(".validate").hide(); } }); $('#theForm').on('submit', function(e) { let phone = $('#phone').val(); let email = $('#email').val(); if (isDataInUse(phone, email)) { // validation failed. cancel the event console.log("not submitting"); e.preventDefault(); return false; } }) function isDataInUse(phone, email) { return (phone === "1234" && email === "1234@gmail.com") }
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <form action='' method='POST' id="theForm"> <div class="validate"><span style="color: red;"><b>Phone and Email Matches!</b></span></div> <input type="phone" name='phone' required='' id="phone" placeholder="0000-000-0000" /> <br/><br/> <input type="email" name='email' required='' id="email" placeholder="hello@youremail.com" /> <br/><br/> <div class="validate"> <button href='/' type='submit' id="submitForm">Submit</button> </div> </form>

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

相关问题 我可以在两个不同的组中验证一个字段吗? - Can I validate one field in two different groups? 使用javascript验证文本字段中的输入(需要为两个值之一) - Validate input in a text field with javascript (needs to be one of two values) 如何使用javascript将Acrobat中的两个字段合并为一个字段 - How to merge a series of two fields into a series of one field in Acrobat with javascript 如何使用JavaScript验证表单中的多个字段? - How can I validate multiple fields in a form using Javascript? Javascript 验证输入字段 + Select 字段 - Javascript Validate Input fields + Select Field 验证是否填写了两个表单字段之一? - Validate if one of two form fields is filled? 给定两个是/否字段,当字段 1 值被选中“是”时,如何使用 JavaScript 使字段 2 值被选中“是”? - Given two yes/no fields, how can I use JavaScript to make the field 2 value checked "Yes" whenever the field 1 value is checked "Yes"? Javascript比较和验证两个文本字段 - Javascript compare and validate two text fields 如何建立验证html中至少一个输入字段的方法? - How can I establish to validate at least one input field in html? jQuery验证:验证一个字段或一对字段中的两个字段是否都必需 - jQuery Validate: Validate that one field, or both fields of a pair are required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM