简体   繁体   English

如何添加Validate()函数?

[英]How to add Validate() function?

I have this form: 我有这个表格:

<form method='post' id='registration'>
...
<input type='submit' value='Submit' />
</form>

And a script which send the form via POST: 还有一个通过POST发送表单的脚本:

<script>
    $(document).ready(function(){
    $( "#registration" ).submit(function(event) {

        // Stop form from submitting normally
        event.preventDefault();

        // Get some values from elements on the page:
        var form = $(this);
        var username = form.find( "input[name='username']" ).val();
        var password = form.find( "input[name='password']" ).val();
        var url = "action.php";

        // Send the data using post
        $.post( url, {
              username: username,
              password: password
        });
        });
    });
</script>

I have written Validate() function also. 我也写了Validate()函数。 Where is the right place to add it and how? 添加它的正确位置在哪里以及如何添加?

I'd personally do it right before the post. 我会在发布之前亲自做。

if (validate()) {
    $.post();
{

This requires your function validate() to return either false or true if the form is valid. 这要求您的函数validate()如果表单有效,则返回false或true。

@Rebirth is quite right. @重生是正确的。

@Nikolay Tsonev, Why don't you put @Nikolay Tsonev,你为什么不放

    var form = $(this);
    var username = form.find( "input[name='username']" ).val();
    var password = form.find( "input[name='password']" ).val();
    var url = "action.php";

into your Validate() function. 到您的Validate()函数中。 and it would just go like directly like; 它会像直接一样

event.preventDefault();

if (validate()) {
    $.post();
{

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

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