]> source.dussan.org Git - archiva.git/blob
97898cc880fcf4d5f39ecb0030b198ddaddcc87a
[archiva.git] /
1 <#--
2 /*
3  *
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21 -->
22 <#--
23 START SNIPPET: supported-validators
24 Only the following validators are supported:
25 * required validator
26 * requiredstring validator
27 * stringlength validator
28 * regex validator
29 * email validator
30 * url validator
31 * int validator
32 * double validator
33 END SNIPPET: supported-validators
34 -->
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);
41
42         var errors = false;
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);
54                 errors = true;
55                 <#if validator.shortCircuit>continueValidation = false;</#if>
56             }
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);
60                 errors = true;
61                 <#if validator.shortCircuit>continueValidation = false;</#if>
62             }
63             <#elseif validator.validatorType = "stringlength">
64             if (continueValidation && field.value != null) {
65                 var value = field.value;
66                 <#if validator.trim>
67                     //trim 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);
72                 </#if>
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);
76                     errors = true;
77                     <#if validator.shortCircuit>continueValidation = false;</#if>
78                 }
79             }
80             <#elseif validator.validatorType = "regex">
81             if (continueValidation && field.value != null && !field.value.match("${validator.expression?js_string}")) {
82                 addError(field, error);
83                 errors = true;
84                 <#if validator.shortCircuit>continueValidation = false;</#if>
85             }
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);
89                 errors = true;
90                 <#if validator.shortCircuit>continueValidation = false;</#if>
91             }
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);
95                 errors = true;
96                 <#if validator.shortCircuit>continueValidation = false;</#if>
97             }
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);
105                     errors = true;
106                     <#if validator.shortCircuit>continueValidation = false;</#if>
107                 }
108             }
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);
117                     errors = true;
118                     <#if validator.shortCircuit>continueValidation = false;</#if>
119                 }
120             }
121             </#if>
122         }
123         </#list>
124     </#list>
125
126         return !errors;
127     }
128 </script>
129 </#if>