Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
None
-
None
-
None
Description
The current version of Click provides only required validation,
I wish to make more validation such as length and format in the
client side.
This is an example for TextField.
========
// JavaScript
function validateTextField(id, required, minLength, maxLength, msgs) {
var field = document.getElementById(id);
if (field) {
var value = trim(field.value);
if (required) {
if (value.length == 0)
}
if (minLength > 0) {
if (value.length < minLength)
}
if (maxLength > 0) {
if (value.length > maxLength)
}
setFieldValidColor(field);
return null;
} else
}
========
// TextField
protected final static String VALIDATE_TEXTFIELD_FUNCTION =
"function validate_
+ " var msg = validateTextField(\n"
+ " ''{0}
'',
{1},
{2},
{3}, [''
{4}'',''
{5}'',''
{6}'']);\n"
+ " if (msg) '{'\n"
+ " return msg + ''|
'';\n"
+ " '}' else '
'\n"
+ "'}'\n";
...
public String getValidationJavaScript() {
Object[] args = new Object[7];
args[0] = getId();
args[1] = String.valueOf(isRequired());
args[2] = String.valueOf(getMinLength());
args[3] = String.valueOf(getMaxLength());
args[4] = getMessage("field-required-error", getErrorLabel());
args[5] = getMessage("field-minlength-error",
new Object[]
);
args[6] = getMessage("field-maxlength-error",
new Object[]
);
return MessageFormat.format(VALIDATE_TEXTFIELD_FUNCTION, args);
}
========
Some common codes which make JavaScript might have to be
extracted to the utility class.
Anyway, how do you think about this example?
Is it possible to provide full client-side validation?
Regards.