jQuery:如何提交一个编辑过的用户输入值的表单? [英] jQuery: how to submit a form with edited user inputed values?

查看:184
本文介绍了jQuery:如何提交一个编辑过的用户输入值的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个简单的HTML表单:

 < h3> 
< a href =#>登入< / a>
< / h3>
< div id =tabs-login>
< form method =getaction =./ dev.html>
< fieldset>
<图例>
电子邮件地址:
< / legend>
< input type =textclass =required emailname =login/>
< / fieldset>
< fieldset>
<图例>
密码:
< / legend>
< input type =passwordclass =requiredname =pass/>
< / fieldset>
< input type =submitvalue =Submit/>
< / form>
< / div>

我使用jQuery来验证它:

 < script> 
$(document).ready(function(){$(form)。validate();});
< / script>

我希望在表单子目录中获取用户输入的密码值并将 SHA256 通过这个jQuery插件并提交 email =用户输入值 pass = sha-value 。如何通过jQuery访问验证值并更改它们并发送到原始表单目的地?

第一部分 - 我想指出我认为这不是一个好主意。在客户端实现密码哈希通常是一个糟糕的主意。相反,传递应该以纯文本(通过HTTPS)发送到服务器,然后在存储之前使用您的首选算法对其进行散列。这还有一个好处,就是不会向潜在的攻击者发布广告,因为该代码只存在于服务器上,因此使用了散列方法。



这就是说:你会想要在表单提交之前计算SHA值。

您可以将事件处理程序绑定到密码字段的blur事件,以使用新哈希更新隐藏字段的值。或者您可以将更新逻辑添加到验证代码中。

无论额外代码的位置如何,您都可以更轻松地准备好散列 到表单提交比在第一个表单之后发送第二个表单。

例如

 <! - 将此添加到表单 - > 
< input type =hiddenname =sha_passvalue =/>

//将其添加到文档就绪处理程序
$('[name = pass]')。blur(function(){
$('[name = sha_pass] ').val($ .sha256($(this).val());
});


So for example I have a simple HTML form:

      <h3>
            <a href="#">Login</a>
        </h3>
        <div id="tabs-login">
            <form method="get" action="./dev.html">
                <fieldset>
                    <legend>
                        Email:
                    </legend>
                    <input type="text" class="required email" name="login" />
                </fieldset>
                <fieldset>
                    <legend>
                        Password:
                    </legend>
                    <input type="password" class="required" name="pass" />
                </fieldset>
                <input type="submit" value="Submit" />
            </form>
        </div>

And I use jQuery to validte it:

    <script>
      $(document).ready(function() { $("form").validate(); });
    </script>

I want on form submition to take user inputed password value and take SHA256 from it via this jQuery plugin and submit email=user-inputed-value and pass=sha-value. How to access validated values via jQuery and change them and send to original form destination?

解决方案

First -- I want to point out that I don't think this is a good idea. It's generally a bad idea to implement password hashing on the client side. Instead, the pass should be sent in plain text (over HTTPS) to the server, where it is then hashed using your preferred algorithm before storage. This has the added benefit of not advertising to potential attackers which hashing method is used, since that code exists only on the server.

That said: you're going to want to calculate that SHA value prior to form submit.

You could bind an event handler to the password field's blur event to update a hidden field's value with the new hash. Or you could add the update logic to the validation code.

Regardless of where the extra code goes, it will be much easier for you to prepare the hash prior to the form submit than it will be to send a second submission chasing after the first.

E.g.

<!-- add this to the form -->
<input type="hidden" name="sha_pass" value="" />

// add this to the document ready handler
$('[name=pass]').blur(function() {
    $('[name=sha_pass]').val($.sha256($(this).val());
});

这篇关于jQuery:如何提交一个编辑过的用户输入值的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆