Files
alfresco-community-repo/source/web/scripts/validation.js
Derek Hulley f03cabea25 Merged V3.2 to HEAD
15908: Merged V3.1 to V3.2 (record only)
      15907: Merged V3.2 to V3.1
         15683: Fixed potential concurrency issues in HeartBeat and LicenseComponent
   15972: Merged V3.1 to V3.2
      15429: **Record only** I18N message IDs for JSF dashlets
      15947: ETHREEOH-2565: The content size shown in the preview popup is incorrect
      15952: Part of fix for ETHREEOH-2493 - missing html encoding on webscript 404 html status page.
      15953: Fix for ETHREEOH-2364 - LDAP sync missing last name displayed as null.
      15956: Fix for ETHREEOH-2714 - html encoding in Create New User summary page.
      15957: Merged DEV-TEMPORARY to V3.1
         15865: Fix for ETHREEOH-2673
      15958: Merged DEV-TEMPORARY to V3.1
         15885: ETHREEOH-2615: Localization Issue - Japanese UI
      15959: Merged DEV-TEMPORARY to V3.1
         15882: ETHREEOH-2644: Sending Email using Template throws exception
      15962: Fix for ETHREEOH-2390
      15963: Fix ETHREEOH-1962: Hibernate flush ordering incorrect for alf_qname
      15965: Merged DEV/BELARUS/V3.1
         15949: ETHREEOH-2689: When upgrading from 2.1.7e to 3.1.0e/3.1.1e user roles are mapped incorrectly
      15966: Merged V2.2 to V3.1
         15948: (RECORD ONLY) Backport of the fix provided for ETHREEOH-1719.
      15969: Prep for ETHREEOH-2295 fixes and associated merges.
      15970: Merged DEV-TEMPORARY to V3.1
         14360: ETHREEOH-1571, ETHREEOH-1656, ETHREEOH-1404, ETHREEOH-953, ALFCOM-2650, ETHREEOH-1572, ETHREEOH-1574
         15331: ETHREEOH-2295 - LayeredFile: Layer file/folder is present when original file/folder is removed
         15824: ETHREEOH-2295 - higlight stale object
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /alfresco/BRANCHES/DEV/BELARUS/V3.1:r15865,15882,15885,15949
   Merged /alfresco/BRANCHES/DEV/3.1SP2:r14360,15331,15824
   Merged /alfresco/BRANCHES/V2.2:r15948
   Merged /alfresco/BRANCHES/V3.1:r15907,15947-15962,15964-15970
   Merged /alfresco/BRANCHES/V3.2:r15908,15972


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@16887 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2009-10-13 20:33:02 +00:00

267 lines
7.0 KiB
JavaScript

//
// Validation functions
// Gavin Cornwell 30-11-2005
//
/**
* Informs the user of the given 'message', if 'showMessage' is true.
* If 'showMessage' is true focus is given to the 'control'.
*/
function informUser(control, message, showMessage)
{
if (showMessage)
{
alert(message);
control.focus();
}
}
/**
* Ensures the value of the 'control' is not null or 0.
*
* @return true if the mandatory validation passed
*/
function validateMandatory(control, message, showMessage)
{
var result = true;
if (control.value == null || control.value.length == 0)
{
informUser(control, message, showMessage);
result = false;
}
return result;
}
/**
* Ensures the value of the 'control' is more than 'min' and less than 'max'.
*
* @return true if the number range validation passed
*/
function validateNumberRange(control, min, max, message, showMessage)
{
var result = true;
if (isNaN(control.value) || control.value < min || control.value > max)
{
informUser(control, message, showMessage);
result = false;
}
return result;
}
/**
* Ensures the value of the 'control' is a number.
*
* @return true if the value is a number
*/
function validateIsNumber(control, message, showMessage)
{
var result = true;
if (isNaN(control.value))
{
informUser(control, message, showMessage);
result = false;
}
return result;
}
/**
* Ensures the value of the 'control' has a string length more than 'min' and less than 'max'.
*
* @return true if the string length validation passed
*/
function validateStringLength(control, min, max, message, showMessage)
{
var result = true;
if (control.value.length < min || control.value.length > max)
{
informUser(control, message, showMessage);
result = false;
}
return result;
}
/**
* Ensures the value of the 'control' matches the 'expression' if 'requiresMatch' is true.
* Ensures the value of the 'control' does not match the 'expression' if 'requiresMatch' is false.
*
* @return true if the regex validation passed
*/
function validateRegex(control, expression, requiresMatch, matchMessage, noMatchMessage, showMessage)
{
var result = true;
var pattern = new RegExp(decode(expression));
var matches = pattern.test(control.value);
if (matches != requiresMatch)
{
if (requiresMatch)
{
informUser(control, noMatchMessage, showMessage);
}
else
{
informUser(control, matchMessage, showMessage);
}
result = false;
}
return result;
}
/**
* Ensures the value of the 'control' does not contain any illegal characters.
*
* @return true if the file name is valid
*/
function validateName(control, message, showMessage)
{
var result = true;
var pattern = /([\"\*\\\>\<\?\/\:\|]+)|([ ]+$)|([\.]?[\.]+$)/;
var trimed = control.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var idx = trimed.search(pattern);
if (idx != -1)
{
informUser(control, "'" + trimed.charAt(idx) + "' " + message, showMessage);
result = false;
}
return result;
}
function validateDialog()
{
if (finishButtonPressed)
{
finishButtonPressed = false;
var message = (window.gecko) ? $("dialog:dialog-body:validation_invalid_character").textContent : $("dialog:dialog-body:validation_invalid_character").innerText;
return validateName($("dialog:dialog-body:name"), message, true);
}
else
{
return true;
}
}
function validateWizard()
{
if (finishButtonPressed)
{
finishButtonPressed = false;
var message = (window.gecko) ? $("wizard:wizard-body:validation_invalid_character").textContent : $("wizard:wizard-body:validation_invalid_character").innerText;
return validateName($("wizard:wizard-body:name"), message, true);
}
else
{
return true;
}
}
/**
* Decodes the given string
*
* @param str The string to decode
* @return The decoded string
*/
function decode(str)
{
var s0, i, j, s, ss, u, n, f;
s0 = ""; // decoded str
for (i = 0; i < str.length; i++)
{
// scan the source str
s = str.charAt(i);
if (s == "+")
{
// "+" should be changed to SP
s0 += " ";
}
else
{
if (s != "%")
{
// add an unescaped char
s0 += s;
}
else
{
// escape sequence decoding
u = 0; // unicode of the character
f = 1; // escape flag, zero means end of this sequence
while (true)
{
ss = ""; // local str to parse as int
for (j = 0; j < 2; j++ )
{
// get two maximum hex characters for parse
sss = str.charAt(++i);
if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f")) || ((sss >= "A") && (sss <= "F")))
{
ss += sss; // if hex, add the hex character
}
else
{
// not a hex char., exit the loop
--i;
break;
}
}
// parse the hex str as byte
n = parseInt(ss, 16);
// single byte format
if (n <= 0x7f) { u = n; f = 1; }
// double byte format
if ((n >= 0xc0) && (n <= 0xdf)) { u = n & 0x1f; f = 2; }
// triple byte format
if ((n >= 0xe0) && (n <= 0xef)) { u = n & 0x0f; f = 3; }
// quaternary byte format (extended)
if ((n >= 0xf0) && (n <= 0xf7)) { u = n & 0x07; f = 4; }
// not a first, shift and add 6 lower bits
if ((n >= 0x80) && (n <= 0xbf)) { u = (u << 6) + (n & 0x3f); --f; }
// end of the utf byte sequence
if (f <= 1) { break; }
if (str.charAt(i + 1) == "%")
{
// test for the next shift byte
i++ ;
}
else
{
// abnormal, format error
break;
}
}
// add the escaped character
s0 += String.fromCharCode(u);
}
}
}
return s0;
}