【问题标题】:Laravel validation for a number which should be exactly 10 digitsLaravel 验证一个应该正好是 10 位数字的数字
【发布时间】:2016-12-06 05:25:07
【问题描述】:

我正在尝试验证一个包含电话号码的表单,我们希望它正好是 10 位数字。但即使输入正确也会出现错误我在控制器中使用以下代码,因此请告知需要更改的内容。

public function tytraining(Request $request)
    {

        $this->validate($request, [
            'txt_name'  => 'required',
            'txt_email' => 'required|email',
            'txt_phone' => 'required|numeric|between:9,11'
        ]);

        $name = Input::get('txt_name');

        $email = Input::get('txt_email');

        $phone = Input::get('txt_phone');

        $type = "bls-training";
        $url = '?' . Input::get('url');

        $medicalRequest = new ParseObject("MedicalRequest");

        $medicalRequest->set("name", $name);
        $medicalRequest->set("email", $email);
        $medicalRequest->set("phone", $phone);
        $medicalRequest->set("type", $type);
        $medicalRequest->set("requestMessage", "training");
        $medicalRequest->set("url", $url);

        try {
            $medicalRequest->save();

        } catch (ParseException $ex) {
            echo(' Some Error Occured');
        }
    }

请告知我应该如何通过 Laravel 进行验证..

提前感谢大家花费宝贵的时间并解决我的问题并帮助我。

【问题讨论】:

  • 当你说“给出错误”时。什么错误?它似乎没有通过验证还是实际错误?
  • 您不会将手机号码作为整数保存在数据库中,所以如果您将其转换为字符串,那么您可以尝试一下:https://laravel.com/docs/5.3/validation#rule-size

标签: php laravel


【解决方案1】:

对于字符串长度(精确):

 public function rules()
    {
        return [
            'input' => 'required|string|size:10'
        ];

对于数字长度(精确):

public function rules()
        {
            return [
                'input' => 'required|string|digits:10'
            ];
        }

对于字符串范围:

   public function rules()
            {
                return [
                    'input' => 'required|string|min:5|max:10'
                ];
        }

对于数字范围:

   public function rules()
            {
                return [
                    'input' => 'required|digits_between:5,10'
                ];
        }

【讨论】:

    【解决方案2】:

    使用大小和数字验证 https://laravel.com/docs/5.3/validation#rule-digits

    $this->validate($request, [
                'txt_name'  => 'required',
                'txt_email' => 'required|email',
                'txt_phone' => 'required|digits:10'
            ]);
    

    【讨论】:

    • 我使用的是 laravel 5.2,我在这里看不到任何数字名称的验证..
    • 是的,Sam 在那里,但它没有验证号码。我暂时使用 strlen() 进行验证并希望有更好的解决方案。
    • 其实in the background it uses strlen,真的。甚至 i ever asked 为什么它总是计算浮点数上的点,而不仅仅是数字。
    【解决方案3】:

    你没有提到你的 Laravel 版本,但至少从 Laravel 4.2 开始你有 digits rule 这就是你需要的,所以你的电话规则应该是这样的:

    'txt_phone' => 'required|numeric|digits:10'
    

    【讨论】:

    • 我使用的是 laravel 5.2,我在这里看不到任何数字名称的验证..
    • 对不起,数字验证在那里,但它不起作用
    【解决方案4】:

    如果你正在验证一个字符串,你可以使用 Laravel 的'size:N' rule:

    对于字符串数据,value 对应于字符数。对于数值数据,值对应于给定的整数值。对于数组,大小对应于数组的计数。对于文件,大小对应于以千字节为单位的文件大小。

    例如:

    public function rules()
    {
        return [
            'token' => 'required|string|size:32'
        ];
    }
    

    【讨论】:

      【解决方案5】:

      您应该使用此验证来验证 9 - 11 位或仅 10 位数字之间的电话/手机号码。

      'txt_phone' => 'numeric|digits_between:9,11'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-04
        • 2017-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-23
        • 1970-01-01
        相关资源
        最近更新 更多