4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 START SNIPPET: supported-validators
24 Only the following validators are supported:
26 * requiredstring validator
27 * stringlength validator
33 END SNIPPET: supported-validators
35 <#if ((parameters.validate?default(false) == true) && (parameters.performValidation?default(false) == true))>
36 <script type="text/javascript">
37 function validateForm_${parameters.id?replace('[^a-zA-Z0-9_]', '_', 'r')}() {
38 form = document.getElementById("${parameters.id}");
39 clearErrorMessages(form);
40 clearErrorLabels(form);
43 var continueValidation = true;
44 <#list parameters.tagNames as tagName>
45 <#list tag.getValidators("${tagName}") as validator>
46 // field name: ${validator.fieldName}
47 // validator name: ${validator.validatorType}
48 if (form.elements['${validator.fieldName}']) {
49 field = form.elements['${validator.fieldName}'];
50 var error = "${validator.getMessage(action)?js_string}";
51 <#if validator.validatorType = "required">
52 if (field.value == "") {
53 addError(field, error);
55 <#if validator.shortCircuit>continueValidation = false;</#if>
57 <#elseif validator.validatorType = "requiredstring">
58 if (continueValidation && field.value != null && (field.value == "" || field.value.replace(/^\s+|\s+$/g,"").length == 0)) {
59 addError(field, error);
61 <#if validator.shortCircuit>continueValidation = false;</#if>
63 <#elseif validator.validatorType = "stringlength">
64 if (continueValidation && field.value != null) {
65 var value = field.value;
68 while (value.substring(0,1) == ' ')
69 value = value.substring(1, value.length);
70 while (value.substring(value.length-1, value.length) == ' ')
71 value = value.substring(0, value.length-1);
73 if ((${validator.minLength?c} > -1 && value.length < ${validator.minLength?c}) ||
74 (${validator.maxLength?c} > -1 && value.length > ${validator.maxLength?c})) {
75 addError(field, error);
77 <#if validator.shortCircuit>continueValidation = false;</#if>
80 <#elseif validator.validatorType = "regex">
81 if (continueValidation && field.value != null && !field.value.match("${validator.expression?js_string}")) {
82 addError(field, error);
84 <#if validator.shortCircuit>continueValidation = false;</#if>
86 <#elseif validator.validatorType = "email">
87 if (continueValidation && field.value != null && field.value.length > 0 && field.value.match(/\b(^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b/gi)==null) {
88 addError(field, error);
90 <#if validator.shortCircuit>continueValidation = false;</#if>
92 <#elseif validator.validatorType = "url">
93 if (continueValidation && field.value != null && field.value.length > 0 && field.value.match(/^(ftp|http|https):\/\/((%[A-F0-9]{2}|[A-Z0-9-._~!$&'()*+,;=:])+@)?((%[A-F0-9]{2}|[A-Z0-9-._~!$&'()*+,;=])+)(:[0-9]+)?((\/(%[A-F0-9]{2}|[A-Z0-9-._~!$&'()*+,;=:@])*)*)(\?(%[A-F0-9]{2}|[A-Z0-9-._~!$&'()*+,;=:@/?])*)?(#(%[A-F0-9]{2}|[A-Z0-9-._~!$&'()*+,;=:@/?])*)?$/gi)==null) {
94 addError(field, error);
96 <#if validator.shortCircuit>continueValidation = false;</#if>
98 <#elseif validator.validatorType = "int">
99 if (continueValidation && field.value != null) {
100 if (<#if validator.min??>parseInt(field.value) <
101 ${validator.min?c}<#else>false</#if> ||
102 <#if validator.max??>parseInt(field.value) >
103 ${validator.max?c}<#else>false</#if>) {
104 addError(field, error);
106 <#if validator.shortCircuit>continueValidation = false;</#if>
109 <#elseif validator.validatorType = "double">
110 if (continueValidation && field.value != null) {
111 var value = parseFloat(field.value);
112 if (<#if validator.minInclusive??>value < ${validator.minInclusive}<#else>false</#if> ||
113 <#if validator.maxInclusive??>value > ${validator.maxInclusive}<#else>false</#if> ||
114 <#if validator.minExclusive??>value <= ${validator.minExclusive}<#else>false</#if> ||
115 <#if validator.maxExclusive??>value >= ${validator.maxExclusive}<#else>false</#if>) {
116 addError(field, error);
118 <#if validator.shortCircuit>continueValidation = false;</#if>