Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,16 +882,21 @@ This validates that a string value can successfully be parsed into a boolean wit

# Number

This validates that a string value contains number values only.
For integers or float it returns true.
This validates that a string value is nonempty and contains only ASCII digits
(0-9). Strings containing a sign or decimal point are not valid; use "numeric"
for those strings.

For values with an integer or floating-point type, it returns true.

Usage: number

# Numeric

This validates that a string value contains a basic numeric value.
basic excludes exponents etc...
for integers or float it returns true.
This validates that a string value contains ASCII digits (0-9), optionally
preceded by a sign (+ or -) and optionally followed by a decimal point and one or
more digits. Exponents are not supported.

For values with an integer or floating-point type, it returns true.

Usage: numeric

Expand Down
24 changes: 24 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package validator_test

import (
"fmt"

"github.com/go-playground/validator/v10"
)

func ExampleValidate_Var_numberAndNumeric() {
validate := validator.New()

for _, value := range []any{"1", "-1", "1.618", 1.618, "1e3"} {
fmt.Printf("%#v: number=%t, numeric=%t\n", value,
validate.Var(value, "number") == nil,
validate.Var(value, "numeric") == nil)
}

// Output:
// "1": number=true, numeric=true
// "-1": number=false, numeric=true
// "1.618": number=false, numeric=true
// 1.618: number=true, numeric=true
// "1e3": number=false, numeric=false
}
Loading