【问题标题】:Using HTML form field validation使用 HTML 表单字段验证
【发布时间】:2015-12-30 18:08:44
【问题描述】:

我正在使用 jQuery Mobile 并尝试使用 HTML5 表单字段验证来执行内联表单字段验证。我这样做是因为我真的很喜欢浏览器在气泡中报告问题的方式,而且我认为等到有人填写完表格然后告诉他们出了什么问题,这对用户来说并不友好。这是我的 HTML:

<form id="frmMain" action="#">
    <input type="checkbox" data-enhance="false" value="1" id="cbxFB" />
    <label for="cbxFB">
        <span class="formsubtext">Check this box to use Facebook information to help fill out this registration.  Once registered you will be able to use the Facebook login button.</span>
    </label>

    <label for="tbEmail">*Email</label><input type="email" id="tbEmail" required autofocus placeholder="example@address.com" />
    <label for="tbPassword">*Password</label><input type="password" id="tbPassword" required />
    <div class="formsubtext" style="margin-top:1px; padding-top:0px; margin-bottom:10px">Minimum of 6 characters, one capital character, and one lower case character.</div>
    <label for="tbPasswordConfirm">*Password Confirm</label><input type="password" id="tbPasswordConfirm" required />

    <label for="tbPin">*Account Pin</label><input type="password" pattern="[0-9]{4}" id="tbPin" required placeholder="####" />
    <div class="formsubtext" style="margin-top:1px; padding-top:0px; margin-bottom:10px">A four digit number that you will remember.  This value will be needed to perform sensitive tasks within the application.</div>

    <label for="tbFName">*First Name</label><input type="text" id="tbFName" required />
    <label for="tbLName">*Last Name</label><input type="text" id="tbLName" required />
    <label for="tbPhone">Phone Number</label><input type="tel" id="tbPhone" pattern="\d{3}[\-]\d{3}[\-]\d{4}" placeholder="###-###-####" style="margin-bottom:1px; padding-bottom:0px;" />
    <div class="formsubtext" style="margin-top:1px; padding-top:0px; margin-bottom:20px;">Used at your option when you schedule an appointment with a service provider</div>

    <div style="display:none;"><label for="tbfbID">Facebook ID</label><input type="text" id="tbfbID" /></div>

    <input type="submit" id="btnMainNext" data-icon="arrow-r" data-iconpos="right" value="Next" data-theme="c" class="ui-btn-c ui-btn ui-corner-all" />
</form>

对于确认密码表单字段,我定义了以下事件:

$("#tbPasswordConfirm").on("change", function (event) {
  var password = $("#tbPassword").val();
  var passwordconfirm = $("#tbPasswordConfirm").val();

  if (password != passwordconfirm) {
    $("#tbPasswordConfirm")[0].setCustomValidity("The value entered does not match the previous password entered.");
    $("#btnMainNext").click();
  }
  else {
    $("#tbPasswordConfirm")[0].setCustomValidity("");
  }
  $(this).focus().select();
})

我的问题是,当用户在字段中输入内容并移至下一个字段时,HTML 表单验证会显示下一个字段的错误消息(这是必需的)。我希望它显示他们刚刚离开的字段的消息。如何阻止焦点移动到下一个字段,以便显示的气泡消息来自他们刚刚输入数据的字段?如您所见,我尝试设置焦点,但这不起作用。任何帮助将不胜感激。

【问题讨论】:

  • 看来你不应该做$("#btnMainNext").click();为什么不做onkeyup呢?
  • 而不是$(this).focus 怎么样$('#tbPasswordConfirm').focus()
  • @billy 为什么? $(this) 是 #tbPasswordConfirm
  • @mplungjan - 对单击按钮的调用是实际触发表单验证的原因。该按钮是提交按钮,如果您直接调用提交函数,则不会触发验证,因此您必须模拟某人单击提交按钮。
  • @billy - 我已经尝试使用您建议的 ID,但没有成功。

标签: javascript jquery html forms validation


【解决方案1】:

您可以阻止焦点移动到下一个字段,但除非您单击提交按钮,否则您无法触发原生验证 UI 或错误消息。

要阻止焦点移动下一个字段,在字段上设置自定义有效性后,您可以使用:

$('#tbPasswordConfirm').blur(function(event) {
    event.target.checkValidity();
}).bind('invalid', function(event) {
    setTimeout(function() { $(event.target).focus();}, 50);
});

blur() 函数将检查模糊的有效性,如果无效,bind() 中的相应函数会将焦点设置回该元素。

【讨论】:

  • 这样可以保持专注于该领域。我所做的就是将此与 mplungjan 提供的内容结合起来,然后在更改事件中触发提交按钮的单击。这给了我我正在寻找的一个小细节。在我将任何字段的有效性设置为无效后,只要字段获得焦点,就会显示气泡帮助及其验证。如何重置表单有效性?顺便说一句,感谢所有为此提供帮助的人。这几天我一直在琢磨它,并没有想出一个好的解决方案。
【解决方案2】:

解决了

Fiddle

$(function() {
  $("#tbPasswordConfirm").on("input", function(event) {
    var thisField = $("#tbPasswordConfirm")[0],
      theForm = $("#frmMain")[0],
      password = $("#tbPassword").val(),
      passwordconfirm = $(this).val(),
      custom = password === passwordconfirm ? "" : "The value entered does not match the previous password entered.";
    thisField.setCustomValidity(custom);
    if (!theForm.checkValidity()) theForm.reportValidity();
  });
});

【讨论】:

  • 我试过这个,但问题是在提交表单之前验证永远不会被触发。所以我所做的是在更改事件中触发了提交按钮的点击,但它似乎与我所做的结果相同。焦点移动到下一个字段并弹出该字段的消息。
  • 我试过了,但是 checkValidity 调用在调用时没有效果。我不记得为什么,但出于某种原因,我记得您必须通过单击提交按钮来模拟表单的提交。
  • 我现在知道怎么做了。 checkValidity 会返回错误,然后我们可以reportValidity。查看更新
  • 它不喜欢 theForm.reportValidity()。告诉我“对象不支持属性或方法'reportValidity'”我查看并看到该函数确实存在于表单上,但它不喜欢它。另一个问题是,由于输入事件会在我开始输入时触发,并且我假设在每次击键时不会在他们仍在输入时显示错误?
  • 是的,在我的 Chrome 中,当密码匹配时,它会跳到下一个字段
【解决方案3】:

您可以使用 html tabindex attr 来操作当您单击制表符时哪个元素将获得焦点。请参阅docs 了解如何使用它。

例如,如果您将密码确认输入为tabindex="5",则可以将tabindex="6" 添加到&lt;label for="tbPin"&gt; 元素中,以防止下一个输入紧随其后。

【讨论】:

  • 我试过了,但它仍然显示下一个字段的验证消息。由于这是在移动设备上,因此标签的选项卡被跳过,因为它与单击每个字段以输入数据的人基本相同。我知道我在我的问题中说错了,并使用了“标签”这个短语。对不起,习惯的力量。
  • 我认为它仍然可以工作。问题可能是标签有for attr。因此,只需尝试将 tabindex="6" 赋予另一个独立元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 2012-06-28
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多