Laravel 验证一个应该正好是 10 位数字的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40988383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:52:54  来源:igfitidea点击:

Laravel validation for a number which should be exactly 10 digits

phplaravel

提问by Rinku Bhilota

I am trying to validate a form which has phone number and we want that to be exactly 10 digits. But it gives errors even when the input is right I am using the following code in the controller so please advise as to what needs to be changed.

我正在尝试验证一个包含电话号码的表单,我们希望它正好是 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');
        }
    }

Please advise how should I do the validation through Laravel..

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

Thank you all in advance for spending your valuable time and going through my concern and helping me out.

预先感谢大家花费宝贵的时间,经历我的担忧并帮助我。

回答by Sagar Rabadiya

use size and digits validation https://laravel.com/docs/5.3/validation#rule-digits

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

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

回答by Marcin Nabia?ek

You haven't mentioned your Laravel version but since at least Laravel 4.2 you have digits ruleand this is what you need here, so your phone rule should look like this:

你还没有提到你的 Laravel 版本,但至少从 Laravel 4.2 你有数字规则,这就是你在这里需要的,所以你的电话规则应该是这样的:

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

回答by Aleksandar

If You are validating a string, You can use Laravel's 'size:N'rule:

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

For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.

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

For example:

例如:

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

回答by Vishal Thakur

You should use this Validation to validate the Phone / Mobile Number between 9 - 11 or only 10 digits.

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

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