JQuery验证程序插件电话验证 [英] JQuery Validator Plugin Phone Validation

查看:79
本文介绍了JQuery验证程序插件电话验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在验证电话号码的jquery验证程序上遇到问题.因此,在我可以输入电话号码的形式中,我使用jquery的其他方法来验证电话.这是代码:

I'm having issues with the jquery validator validating phone numbers. So in a form where I have an input for phone number, I use jquery's additional methods to validate the phone. Here's the code:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>stuff/title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(e) {
    var val = $('#everything').validate({
        onkeyup: false,
        errorClass: "req_mess",
        ignore: ":hidden",
        rules: {
            phone: {
                required: true,
                minlength: 10,
                phoneUS: true
            }
        },
        messages: {
            phone: {
                required: "Please enter your phone number",
                phoneUS: "Please enter a valid phone number: (e.g. 19999999999 or 9999999999)"
            }
        }
    });
});
</script>
<form name="everything" id="everything" action="Private/Insert.php" method="post" style="display:none;" >
    Phone Number: <input type="text" name="phone" id="phone"/>
</form>
</body>
</html>

电话号码列的数据库最终看起来像这样:

有时,它会正确插入完整的电话号码(以红色划线),但有时会只插入3位数.我不知道为什么这可能与Internet Explorer有关吗?有人遇到过这个问题吗?有人知道为什么会这样吗?如果有人需要它

And the database ends up looking like this for the phone number column:

Sometimes it inserts the full phone number (crossed out in red) properly, but then sometimes it just inserts 3 digits. I don't know why. Is this something to do with Internet Explorer maybe? Has anyone come across this problem? Does anyone know why this is happening? Here's the entirety of the jquery validation code if anyone needs it

推荐答案

您正在使用 phoneUS 电话号码的验证方法,该方法接受以下两个输入(示例):

You are using the phoneUS validation method for the phone number, which accepts both of the inputs below (examples):

  • 949-101-2020
  • 9491012020

由于第一个示例被视为有效,因此您尝试将其直接存储到数据库中,并且似乎正在使用整数或数字列.包括MySQL在内的许多数据库都将尝试通过简单地将第一个非数字字符后的所有内容删除来将该值强制转换为整数.我总是建议使用varchar字段来存储电话号码,因为这些号码实际上并没有数字含义(即,您永远不会加/减).

Since the first example is considered valid, you attempt to store it directly into your database, and it seems that you are using an integer or number column. Many databases, including MySQL will attempt to coerce that value into an integer by simply dropping everything after the first non-numeric character. I always recommend using a varchar field for storing phone numbers because the numbers don't really have a numeric meaning (ie you're never going to add/subtract).

此外,仅依靠客户端验证也是一个坏主意.您总是要在服务器端验证数据,因为欺骗客户端验证(无论是否恶意)都非常简单.

Also, it's a bad idea to simply rely on client side validation. You always want to validate your data on the server side because it's very trivial to trick client side validation (maliciously or not).

这篇关于JQuery验证程序插件电话验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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