Laravel validate decimal 0-99.99
The Laravel
2nd...................
Arrrgh! Don't listen to the regular expression answers. RegEx is icky for this, and I'm not talking just performance. It's so easy to make subtle, impossible to spot mistakes with your regular expression.
If you can't use
The
The check on the length is for a special case involving empty strings. Also note that it falls down on your 0x89f test, but that's because in many environments that's an okay way to define a number literal. If you want to catch that specific scenario you could add an additional check. Even better, if that's your reason for not using
The Laravel
between
validation rule is actually pretty powerful and can handle decimal values as well. So there's no need to use regex just do this:'required|between:0,99.99'
function rules(){
return [
'field_name'=> array('regex:/^(?:d*.d{1,2}|d+ )$/','min:1','max:10')
];
}
Validate decimal numbers in JavaScript - IsNumeric()
// Whitespace strings:
IsNumeric(' ') == true;
IsNumeric('\t\t') == true;
IsNumeric('\n\r') == true;
// Number literals:
IsNumeric(-1) == false;
IsNumeric(0) == false;
IsNumeric(1.1) == false;
IsNumeric(8e5) == false;
Some time ago I had to implement an IsNumeric
function, to find out if a variable contained a numeric value, regardless of its type, it could be a String
containing a numeric value (I had to consider also exponential notation, etc.), a Number
object, virtually anything could be passed to that function, I couldn't make any type assumptions, taking care of type coercion (eg. +true == 1;
but true
shouldn't be considered as "numeric"
).2nd...................
Arrrgh! Don't listen to the regular expression answers. RegEx is icky for this, and I'm not talking just performance. It's so easy to make subtle, impossible to spot mistakes with your regular expression.
If you can't use
isNaN()
, this should work much better:function IsNumeric(input)
{
return (input - 0) == input && (''+input).trim().length > 0;
}
Here's how it works:The
(input - 0)
expression forces JavaScript to do type coercion on your input value; it must first be interpreted as a number for the subtraction operation. If that conversion to a number fails, the expression will result in NaN
. This numeric result is then compared to the original value you passed in. Since the left hand side is now numeric, type coercion is again used. Now that the input from both sides was coerced to the same type from the same original value, you would think they should always be the same (always true). However, there's a special rule that says NaN
is never equal to NaN
, and so a value that can't be converted to a number (and only values that cannot be converted to numbers) will result in false. The check on the length is for a special case involving empty strings. Also note that it falls down on your 0x89f test, but that's because in many environments that's an okay way to define a number literal. If you want to catch that specific scenario you could add an additional check. Even better, if that's your reason for not using
isNaN()
then just wrap your own function around isNaN()
that can also do the additional check.Laravel set between digits in validation
'required|integer|digits_ between:1,10'
or'display_post_count' => 'required|min:1|max:10',
0 Comments