【问题标题】:Two different validation to a form对表单的两种不同验证
【发布时间】:2023-03-04 09:45:01
【问题描述】:

我有一个包含提交按钮的表单。我正在使用 form.validation 插件

我已经制定了规则。没关系!

但现在我创建了一个格式不正确的链接,当用户点击时,我想更改这些规则。有可能吗?

例如:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<script type="text/javascript">
$().ready(function() {
    $(".myform").validate({
        rules: {
            name: {
                required: true
            },
            email: {
                required: true
            },
        }
    });
});
</script>
<form class="myform">
    Name: <input type="text" name="name">
    Email: <input type="text" name="email">
    Phone: <input type="text" name="phone">
    <input type="submit" value="Send">
</form>
<a href="#">Call me</a>

当用户点击“给我打电话”时,验证将只是“电话”字段。

【问题讨论】:

  • 不知道form.validation插件是什么。
  • 您能否至少发布一些代码或描述您实际尝试过的内容?
  • 这在目前的状态下是无法回答的,您能否在它被炸毁之前发布更多信息或删除问题。 IE有什么规则?代码示例。表单代码。插件代码。随便
  • 现在有更多细节......

标签: php jquery validation


【解决方案1】:

在我看来,单击链接时需要执行以下操作(全部在 javascript / jQuery 中):

  • 取消链接的默认操作;
  • nameemail中删除规则;
  • phone 字段添加新规则;
  • 可选择隐藏除phone 字段以外的所有字段。

您可以在插件文档的rules section 中找到更多详细信息。

【讨论】:

  • 在链接和按钮上粘贴事件监听并修改事件中的验证规则要简单得多。否则规则会丢失在代码中
  • @Nicholas King 在这种情况下这将不起作用(根据手册)validate 方法sets up event handlers for submit, focus, keyup, blur and click to trigger validation of the entire form or individual elements。所以如果你在事件已经触发后绑定它,那就太晚了。
  • 只是阅读文档,但我很确定我下面的方法应该可以稍微玩一下并使用有效方法而不是验证。否则,它就像您建议的那样使用插件规则()方法
  • 虽然如果他只是检查空字段,我认为使用 jquery 检查没有插件会容易得多
  • 我们中的任何一个都有可能得到 Osny Santos Netto 的支持或接受的答案?瘦到没有! :-)
【解决方案2】:

这段代码在语法和结构上都有很多错误

<script type="text/javascript">
$().ready(function() {
$(".myform").validate({
    rules: {
        name: {
            required: true
        },
        email: {
            required: true
        },
    }
});
});
</script>
<form class="myform">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
Phone: <input type="text" name="phone">
</form>
<a href="#">Call me</a>

应该看起来更像

<script type="text/javascript">
$(document).ready(function() {
$("#callme").click(function(){
   $(".myform").validate({
    rules: {
        phone: {
            required: true
        }
    /*note the removed ,*/
    }
});
});
$("#submit").click(function(){
$(".myform").validate({
    rules: {
        name: {
            required: true
        },
        email: {
            required: true
        }
    /*note the removed ,*/
    }
});
});
});
</script>
<!-- Your Form must have a method attribute either post or get //-->
<form class="myform" method="post" action="">
<label for="name">Name:</label> <input type="text" name="name" id="name">
<label for="email">Email:</label> <input type="text" name="email" id="email">
<label for="phone">Phone:</label> <input type="text" name="phone" id="phone">
<!-- Hide this label with css //-->
<label for="submit" class="hidden">submit:</label><input type="submit" value="submit" text="submit" id="submit" />
</form>
<!-- Link this to another form with new js validation rules //-->
<a href="#" id="callme">Call me</a>

【讨论】:

  • 对不起,我知道你给我看的所有错误,我的表单有很多字段,我只适应在stackoverflow中询问。所以我认为没有必要提供所有这些细节。但我已经编辑了。 Tha形式必须相同,这是我的问题,有可能吗?
  • 是的,但你需要了解你写的内容
  • 您需要在元素上执行某种事件。一个在链接上,一个在按钮上
  • 这个工作jsfiddle.net/g3Byh/2如果你接受我的回答,我会很感激
  • 我认为这是您的解决方案stackoverflow.com/questions/1510165/…
【解决方案3】:

这种方法奏效了。在我的测试中,我看到:

  • 不能是链接
  • 不能出去

有人可以确认吗?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#submit1").click(function(){
   $(".myform").validate({
    rules: {
        phone: {
            required: true
        }
    }
});
});
$("#submit").click(function(){
$(".myform").validate({
    rules: {
        name: {
            required: true
        },
        email: {
            required: true
        }
    }
});
});
});
</script>
<!-- Your Form must have a method attribute either post or get //-->
<form class="myform" method="post" action="">
<label for="name">Name:</label> <input type="text" name="name" id="name">
<label for="email">Email:</label> <input type="text" name="email" id="email">
<label for="phone">Phone:</label> <input type="text" name="phone" id="phone">
<!-- Hide this label with css //-->
<input type="submit" value="submit" text="submit" id="submit" />
<input type="submit" value="submit1" text="submit" id="submit1" />
</form>
<!-- Link this to another form with new js validation rules //-->

【讨论】:

  • 这段代码和我下面的代码一样。锚点完全可以提交表单。
  • 这段代码是基于你的,但你的没有用……我只是不明白为什么。
  • 我认为这是使用设置数组的可能解决方案,尽管这似乎是一个常见问题! stackoverflow.com/questions/1510165/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 2020-04-14
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
相关资源
最近更新 更多