-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEmbAJAXValidatingTextInput.h
78 lines (74 loc) · 3.01 KB
/
EmbAJAXValidatingTextInput.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
*
* EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device).
*
* Copyright (C) 2018 Thomas Friedrichsmeier
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**/
#ifndef EMBAJAXVALIDATINGTEXTINPUT_H
#define EMBAJAXVALIDATINGTEXTINPUT_H
#include "EmbAJAX.h"
template<size_t SIZE> class EmbAJAXValidatingTextInput : public EmbAJAXTextInput<SIZE> {
public:
EmbAJAXValidatingTextInput(const char* id) : EmbAJAXTextInput<SIZE>(id) {
_attributes = EmbAJAXBase::null_string;
_placeholder = nullptr;
_pattern = nullptr;
}
void print() const override {
EmbAJAXBase::_driver->printFormatted("<input type=\"text\" id=", HTML_QUOTED_STRING(EmbAJAXTextInput<SIZE>::_id), " maxLength=", INTEGER_VALUE(SIZE-1),
" size=", INTEGER_VALUE(min(max(SIZE, (size_t) 11), (size_t) 41) - 1), " ", PLAIN_STRING(_attributes),
" onInput=\"doRequest(this.id, this.value); this.checkValidity();\"");
if (EmbAJAXTextInput<SIZE>::_value[0] != '\0') {
EmbAJAXBase::_driver->printAttribute("value", EmbAJAXTextInput<SIZE>::_value);
}
if (_placeholder != 0) {
EmbAJAXBase::_driver->printAttribute("placeholder", _placeholder);
}
if (_pattern != 0) {
EmbAJAXBase::_driver->printAttribute("pattern", _pattern);
}
EmbAJAXBase::_driver->printContent("/>");
}
/** Set a placeholder text (will be shown, when the input is empty) */
void setPlaceholder(const char* placeholder) {
_placeholder = placeholder;
}
/** Set a placeholder text (will be shown, when the input is empty)
*
* Example: @code
* setPattern("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"); // IPv4-Address
* @endcode
*/
void setPattern(const char* pattern) {
_pattern = pattern;
}
/** Specify custom attributes (other than placeholder and pattern)
* to be inserted in the \<input>-tag in the generated HTML.
*
* Example: @code
* setCustomAttributes("min=\"1\" max=\"100\"");
* @endcode
*/
void setCustomValidationAttributes(const char* attributes) {
_attributes = attributes;
}
private:
const char* _attributes;
const char* _placeholder;
const char* _pattern;
};
#endif