日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

html form int,is_int, is_numeric, is_float, and HTML form validation

發布時間:2024/9/27 HTML 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html form int,is_int, is_numeric, is_float, and HTML form validation 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題

A select field on my HTML form may yield 1 to 5 (integers). Using is_int rejects it every time, because the $_POST['rating'] is viewed as a string.

After consulting the PHP Manual, it seems is_numeric() && !is_float() is the proper way to validate for an integer in this case.

But I want to be certain, so please confirm or fire away at my logic.

回答1:

I would use is_numeric(), because user input always comes in as a string (as far as I know).

Another way to guarantee something is an integer is to cast it...

$id = (int) $id;

回答2:

I would probably use something like this:

$value = filter_var(

$_POST['rating'],

FILTER_VALIDATE_INT,

array('options' => array('min_range' => 1, 'max_range' => 5)));

filter_var() will return either boolean false if the value is non-integer or out-of-range, or the valid value itself (as an integer.)

回答3:

You could use the following regular expression:

preg_match('/^[0-9]{1,}$/', $value);

I does validate digits with leading zeros though...

回答4:

is_int requires the input content is a integer.

is_numeric requires the input content is a integer or a string including just 0-9.

but I am wondering the result if I put a number that is bigger than PHP_INT_MAX as the parameter into the above 2 functions.

回答5:

You can always cast it as an int.

$rating = (int)$_POST['rating'];

This will remove the need to validate it (even though you should always validate form data).

It may reduce your steps is what I'm getting at.

回答6:

If you're testing for digits only (what an integer usually is ;)), I tend to use ctype_digit instead of is_int. That way, you won't lose data through casting, and you can just use a string:

$_POST['foo'] = '42';

echo ctype_digit( (string) $_POST['foo'] ) ? 'yep' : 'nope';

That'll output "yep".

回答7:

if you want to know if $_POST['rating'] is an int before you even try to cast do use is_numeric() && !is_float() as you have. This will tell you if the string is an int or not. If you just cast to an int and there is a non numeric all the numbers before the first letter in the string is turned into an int.

x = 457h

print (int)x

outputs 457

x = h56

print (int)x

outputs 0

來源:https://stackoverflow.com/questions/5486926/is-int-is-numeric-is-float-and-html-form-validation

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的html form int,is_int, is_numeric, is_float, and HTML form validation的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。