表格验证 - 标准解决方案? [英] Form Validation -- A Standard Solution?

查看:138
本文介绍了表格验证 - 标准解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点高于网络编程的新手,而且我对表单验证还不太了解。在我的研究中,似乎有多种方法可以验证表单数据,而某些方法取决于您将如何处理数据。然后,你必须弃用像> mysql_real_escape_string 和一些名为PDO的东西,整件事情就变得复杂了。



<因此,如果有一组函数基本上是验证表单数据的标准方法,那么是否合理/可行?像这样...

$ p $ 函数validate_for_sql()
函数validate_for_email()
函数validate_for_browser()

也许应该有更多的东西(用于整数?)。理想情况是sql注入等其他问题都可以通过一系列普遍接受的和为编程社区开发的稳定的函数来处理。



是这可行吗?它是否已经存在于某个地方......也许在一个隐藏的巢穴里?如果是这样,有人可以给我发送密码来获取这些信息吗? :)

解决方案

让我总结一下你的问题:对于一组函数来说,合理/

如果我总结的是真的,那么答案是: jQuery Validation 。正如名字所述,您需要使用jQuery和jQuery Validation来完成这项工作。



下面是如何使用这些工具的示例。



HTML:

 < form id =myForm> 
< input type =textname =currency>
< input type =textname =amount>
< / form>

Javascript:

 < code $> $(#myForm)。validate({
rules:{
currency:{
minlength:3,
required:true
},
amount:{
number:true
}
}
});

默认情况下,在KeyUp上触发验证。所以,当用户改变,比如字段货币并破坏了上述规则之一时,jQuery验证会自动给出预生成通知消息。



这些是预生成以上规则的消息:


  1. minlength:请输入至少三个字符





  2. $ b

    如果您想验证汇率表中是否有货币可用,该怎么办?

    答案是:只需添加自定义验证方法即可。

      $ .validator.addMethod(checkCurrency,
    function(value,element){
    var result = false;
    $ .ajax({
    类型:GET,
    async:false,
    url:012_ajax.php,//在服务器端验证脚本
    data:{currency:value},
    success:function(data){
    result =(data ==1)?true:false;
    }
    });
    //如果货币存在于数据库中,则返回true
    返回结果;
    },
    货币不存在。
    );

    $(#myForm)。validate({
    rules:{
    currency:{
    minlength:3,
    required:true ,
    checkCurrency:true
    },
    amount:{
    number:true
    }
    }
    });

    使用此代码,每次用户输入表中的新兴货币时,表单将始终显示货币不存在消息。




    最后一个问题:我可以为这个插件创建自定义消息吗?



    答案为YES。

      $(#myForm)。validate({
    规则:{
    currency:{
    minlength:3,
    required:true,
    checkCurrency:true
    },
    amount:{
    数字:真
    }
    },
    消息:{
    货币:{
    minlength:货币应该至少3个字符或更多 ,需要
    :需要货币
    },
    金额:{
    number:金额必须是数字
    }
    }
    });

    现在,每当用户破坏上述规则时,表单将在上面的代码中将消息呈现为状态。



    这个插件的重要之处在于,只要数据未经过验证,数据将不会被提交。那么,只要用户不关闭/禁用他的浏览器的JavaScript。 :)




    UPDATE



    上面的答案适用于客户端对于服务器端验证,在互联网上有PHP表单验证脚本,其中包括:


    1. http:// html-form -guide.com

    2. Soares
    3. a>

    它们具有内置验证规则,用户可以添加自定义规则。



    希望这有帮助。


    I'm a little above a newbie with web programming and I don't know much about form validation. In my research, it seems that there are a variety of ways to validate form data and some of the methods depend on what you're going to do with the data. Then you have to-be deprecated functions like mysql_real_escape_string and something called "PDO" and the whole thing is downright complicated.

    So, would it be reasonable/feasible for there to be a set of functions that are basically the standard way to validate form data? Like this...

    function validate_for_sql()
    function validate_for_email()
    function validate_for_browser()
    

    Maybe there should be more (something for integers?). The ideal would be things like sql injections and other nasties could ALL be handled via a set of generally accepted and rock-solid functions developed by and for the coding community.

    Is this doable? Does it already exist somewhere.. maybe in a hidden lair? If so, can someone email me the secret password needed to access this information? :)

    解决方案

    Let me summarize your question : "would it be reasonable/feasible for there to be a set of functions, generally accepted, uniform, and solid, that are basically the standard way to validate form data?"

    If what I summarize is true, then the answer is : jQuery Validation. As the name stated, you need jQuery and jQuery Validation to make this work.

    Below is an example on how to use this tools.

    HTML :

    <form id="myForm">
        <input type="text" name="currency">
        <input type="text" name="amount">
    </form>
    

    Javascript:

    $("#myForm").validate({
        rules: {
            "currency": {
                minlength: 3,
                required: true
            },
            "amount": {
                number: true
            }
        }
    });
    

    As default, validation is triggered on KeyUp. So when user change, lets say, field currency and broke one of the rules above, jQuery Validation will give pre-build notification message automatically.

    These are the pre-build messages for rules above :

    1. minlength: Please enter at least three characters
    2. required: This field is required
    3. number: Please enter a valid number


    What if you want to validate whether a currency is available in our exchange rate's table?

    The answer is: just add custom Validation Method.

    $.validator.addMethod("checkCurrency", 
        function(value, element) {
            var result = false;
            $.ajax({
                type:"GET",
                async: false,
                url: "012_ajax.php", // script to validate in server side
                data: {currency: value},
                success: function(data) {
                    result = (data == "1") ? true : false;
                }
            });
            // return true if currency is exist in database
            return result; 
        }, 
        "Currency is not exist."
    );
    
    $("#myForm").validate({
        rules: {
            "currency": {
                minlength: 3,
                required: true,
                checkCurrency: true
            },
            "amount": {
                number: true
            }
        }
    });
    

    With this code, everytime user entry unexist currency in table, form will always show "Currency is not exist" message.


    Last question: could I create custom message for this plugin?

    The answer is YES.

    $("#myForm").validate({
        rules: {
            "currency": {
                minlength: 3,
                required: true,
                checkCurrency: true
            },
            "amount": {
                number: true
            }
        },
        messages: {
            "currency": {
                minlength: "Currency should at least 3 char or more",
                required: "Currency is required"
            },
            "amount": {
                number: "Amount must be number"
            }
        }
    });
    

    Now everytime user broke rules above, form will gives messages as state in code above.

    The important thing of this plugin, as every form validation should do, is that data will not be submitted as long as data are not verified. Well, as long as user is't turning-off / disable javascript from his browser. :)


    UPDATE

    The answer above is for client side validation.

    For server side validation, there are PHP form validation scripts on internet, among them are :

    1. http://html-form-guide.com
    2. A Soares

    They have build-in validation rules and user could add the custom ones.

    Hopefully this help.

    这篇关于表格验证 - 标准解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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