File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
src/main/java/org/javawebstack/validator Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ public class Validator {
3232 registerRuleType ("ipv6" , IPv6AddressRule .class );
3333 registerRuleType ("int" , IntegerRule .class );
3434 registerRuleType ("integer" , IntegerRule .class );
35+ registerRuleType ("numeric" , NumericRule .class );
3536 registerRuleType ("date" , DateRule .class );
3637 registerRuleType ("array" , ArrayRule .class );
3738 registerRuleType ("list" , ArrayRule .class );
Original file line number Diff line number Diff line change 1+ package org .javawebstack .validator .rule ;
2+
3+ import org .javawebstack .abstractdata .AbstractElement ;
4+ import org .javawebstack .validator .ValidationContext ;
5+
6+ import java .lang .reflect .Field ;
7+
8+ public class NumericRule implements ValidationRule {
9+ @ Override
10+ public String validate (ValidationContext context , Field field , AbstractElement value ) {
11+ if (value == null )
12+ return null ;
13+
14+ float v ;
15+ if (value .isNumber ())
16+ v = value .number ().floatValue ();
17+ else if (value .isString ()) {
18+ try {
19+ v = Float .parseFloat (value .string ());
20+ } catch (NumberFormatException ex ) {
21+ return "Not a numeric value" ;
22+ }
23+ } else {
24+ return "Not a numeric value" ;
25+ }
26+
27+ return null ;
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments