MCPcopy Create free account
hub / github.com/charmbracelet/bubbles / ExampleValidateFunc

Function ExampleValidateFunc

textinput/textinput_test.go:143–174  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

141}
142
143func ExampleValidateFunc() {
144 creditCardNumber := New()
145 creditCardNumber.Placeholder = "4505 **** **** 1234"
146 creditCardNumber.Focus()
147 creditCardNumber.CharLimit = 20
148 creditCardNumber.SetWidth(30)
149 creditCardNumber.Prompt = ""
150 // This anonymous function is a valid function for ValidateFunc.
151 creditCardNumber.Validate = func(s string) error {
152 // Credit Card Number should a string less than 20 digits
153 // It should include 16 integers and 3 spaces
154 if len(s) > 16+3 {
155 return fmt.Errorf("CCN is too long")
156 }
157
158 if len(s) == 0 || len(s)%5 != 0 && (s[len(s)-1] < '0' || s[len(s)-1] > '9') {
159 return fmt.Errorf("CCN is invalid")
160 }
161
162 // The last digit should be a number unless it is a multiple of 4 in which
163 // case it should be a space
164 if len(s)%5 == 0 && s[len(s)-1] != ' ' {
165 return fmt.Errorf("CCN must separate groups with spaces")
166 }
167
168 // The remaining digits should be integers
169 c := strings.ReplaceAll(s, " ", "")
170 _, err := strconv.ParseInt(c, 10, 64)
171
172 return err
173 }
174}
175
176func TestShiftBackspaceDeletesCharacterBackward(t *testing.T) {
177 ti := New()

Callers

nothing calls this directly

Calls 3

NewFunction · 0.70
FocusMethod · 0.45
SetWidthMethod · 0.45

Tested by

no test coverage detected