简体   繁体   English

如何停止表单提交并使用javascript进行验证?

[英]How can I stop a form submit and validate with javascript?

I've come across a form that looks like this that I cannot change: 我遇到了一种无法更改的表格:

<form id="mx_locator" name="mx_locator" method="post" action="search-results">

  <!-- form elements -->
  <span><input type="image" src="/images/search.png" onclick="loader()"></span>

</form>

It's inside of a CMS that knows to forward the form data and produce a results page when clicked. 它位于CMS内,该CMS知道如何转发表单数据并在单击时生成结果页。 The loader function call just handles adding/removing a class for some effect. 加载器函数调用仅处理添加/删除类以获得某种效果。

I've been asked to write a javascript that validates the form. 我被要求写一个JavaScript来验证表格。 There is a zip code field and a city field. 有一个邮政编码字段和一个城市字段。 One or the other has to be filled out. 一个或另一个必须填写。 Not both, but one or the other. 不是两者,而是一个或另一个。 So when someone clicks that button my javascript has to stop the submit, check those fields, and only go forward if they're good. 因此,当有人单击该按钮时,我的javascript必须停止提交,检查这些字段,并且只有在条件良好时才继续操作。

In short, how would I go about doing this? 简而言之,我将如何去做呢? More than anything, I'm worried the form will process no matter what the js does, because the CMS is controlling it. 最重要的是,我担心无论js做什么,表单都会处理,因为CMS正在控制它。 I can't change the form html at all. 我根本无法更改html格式。 I can just add the JS. 我可以添加JS。

Start by adding the onsubmit attribute to the form tag. 首先将onsubmit属性添加到表单标签。

<form id="mx_locator" onsubmit="return ziporcity(this)" name="mx_locator" ...

Then add a script tag to your document either containing or linking to the function ziporcity, which you can write like this: 然后将一个脚本标记添加到包含或链接到函数ziporcity的文档中,您可以这样编写:

<script>
function ziporcity(f) { return /\d{5}/.test(f['zipFieldName'].value) || /\S/.test(f['cityFieldName'].value); }
</script>

This simple validation function checks that one or the other or both are true, that the zip field contains 5 consecutive digits (9 digits would also pass), and/or the city field contains non-whitespace. 这个简单的验证功能可检查一个或另一个或两个都为真,zip字段包含5个连续的数字(也将传递9个数字),和/或city字段包含非空格。

You need to change zipFieldName to be the name contained in the name="..." attribute of the zip field in your form, and the cityFieldName to be the name of the city field. 您需要将zipFieldName更改为表单中zip字段的name =“ ...”属性中包含的名称,并将cityFieldName更改为city字段的名称。

<script> 
$(document).ready(function() {
    $("#mx_locator").submit(function() {  
      var isValid = callSomeValidationFunctionThatReturnTrueFalse();
      return isValid;  // the key is return false will prevent submit
    });
});
</script>

You will need to include jQuery. 您将需要包括jQuery。 If something is really weird where your form submit event is being completely overrun by the CMS, then just hide and create a new form in it's place. 如果您的表单提交事件完全被CMS覆盖,这确实很奇怪,那么只需在该位置隐藏并创建一个新表单即可。

<script> 
$(document).ready(function() {
    // save old form html and hide
    var myForm = $("#mx_locator");
    var formHtml = myForm.html();
    myForm.html("").hide();

    // append new form in its place and 
    var myNewForm = $('<form id="mx_locator2" name="mx_locator2" .../></form>');
    myNewForm.html(formHtml);
    myNewForm.insertAfter(myForm);
});
</script>

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

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