Add numeric-only validation support on fields

This commit is contained in:
Dominik Downarowicz committed 2023-10-26 18:08:40 +02:00
1 parent d39b10e79d
commit 54e3008e26
2 files changed
+12 -3

No files matched your search

+3
View File
@@ -33,6 +33,9 @@ var screen1 = go3270.Screen{
{Row: 6, Col: 0, Content: "Password . . . ."},
{Row: 6, Col: 19, Name: "password", Write: true, Hidden: true},
{Row: 6, Col: 40}, // field "stop" character
{Row: 7, Col: 0, Content: "EmployeeId . . . ."},
{Row: 7, Col: 19, Name: "employeeId", Write: true, Highlighting: go3270.Underscore, NumericOnly: true},
{Row: 7, Col: 40}, // field "stop" character
{Row: 8, Col: 0, Content: "Press"},
{Row: 8, Col: 6, Intense: true, Content: "enter"},
{Row: 8, Col: 12, Content: "to submit your name."},
+9 -3
View File
@@ -40,6 +40,9 @@ type Field struct {
// password input field).
Hidden bool
// NumericOnly if set only allows numeric input to be written to the field
NumericOnly bool
// Color is the field color. The default value is the default color.
Color Color
@@ -193,7 +196,7 @@ func buildField(f Field) []byte {
if f.Color == DefaultColor && f.Highlighting == DefaultHighlight {
// this is a traditional field, issue a normal sf command
buf.WriteByte(0x1d) // sf - "start field"
buf.WriteByte(sfAttribute(f.Write, f.Intense, f.Hidden, f.Autoskip))
buf.WriteByte(sfAttribute(f.Write, f.Intense, f.Hidden, f.Autoskip, f.NumericOnly))
return buf.Bytes()
}
@@ -210,7 +213,7 @@ func buildField(f Field) []byte {
// Write the basic field attribute
buf.WriteByte(0xc0)
buf.WriteByte(sfAttribute(f.Write, f.Intense, f.Hidden, f.Autoskip))
buf.WriteByte(sfAttribute(f.Write, f.Intense, f.Hidden, f.Autoskip, f.NumericOnly))
// Write the highlighting attribute
if f.Highlighting != DefaultHighlight {
@@ -228,7 +231,7 @@ func buildField(f Field) []byte {
}
// sfAttribute builds the attribute byte for the "start field" 3270 command
func sfAttribute(write, intense, hidden, skip bool) byte {
func sfAttribute(write, intense, hidden, skip, numeric bool) byte {
var attribute byte
if !write {
attribute |= 1 << 5 // set "bit 2"
@@ -248,6 +251,9 @@ func sfAttribute(write, intense, hidden, skip bool) byte {
attribute |= 1 << 3 // set "bit 4"
attribute |= 1 << 2 // set "bit 5"
}
if numeric {
attribute |= 1 << 4 // set "bit 3"
}
// Fill in top 2 bits with appropriate values
attribute = codes[attribute]
return attribute