[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/js/ -> validation.js (source)

   1  /*
   2   * @(#) $Id: validation.js 3399 2007-11-04 15:30:05Z glen $
   3   */
   4  
   5  last_issue_number_validation_value = '';
   6  function validateIssueNumberField(baseURL, form_name, field_name)
   7  {
   8      form_value = getFormElement(getForm(form_name), field_name).value;
   9      if (last_issue_number_validation_value == form_value) {
  10          return;
  11      } else {
  12          last_issue_number_validation_value = form_value;
  13      }
  14      validate_issue_http_client = new HTTPClient();
  15      validate_issue_http_client.loadRemoteContent(baseURL + '/validate.php?action=validateIssueNumbers&values=' +
  16          form_value + '&field_name=' + field_name + '&form_name=' + form_name + '&check_project=0', 'displayIssueFieldValidation');
  17  }
  18  
  19  function displayIssueFieldValidation(response)
  20  {
  21      var chunks = response.responseText.split(':',3);
  22      f = getForm(chunks[0]);
  23      error_span = getPageElement(chunks[1] + '_error');
  24      if (chunks[2] != 'ok') {
  25          selectField(f, chunks[1]);
  26          error_span.innerHTML = '<b>Error</b>: The following issues are invalid: ' + chunks[2];
  27      } else {
  28          errorDetails(f, chunks[0], false);
  29          error_span.innerHTML = '';
  30      }
  31  }
  32  
  33  function isValidDate(f, field_prefix)
  34  {
  35      var selected_date = new Date();
  36      selected_date.setMonth(getSelectedOption(f, field_prefix + '[Month]')-1);
  37      selected_date.setDate(getSelectedOption(f, field_prefix + '[Day]'));
  38      selected_date.setYear(getSelectedOption(f, field_prefix + '[Year]'));
  39  
  40      if (selected_date.getDate() != getSelectedOption(f, field_prefix + '[Day]')) {
  41          return false;
  42      } else {
  43          return true;
  44      }
  45  }
  46  
  47  function resetForm(f)
  48  {
  49      if (confirm('This action will clear out any changes you performed on this form.')) {
  50          f.reset();
  51          return true;
  52      } else {
  53          return false;
  54      }
  55  }
  56  
  57  function confirmCloseWindow()
  58  {
  59      if (confirm('Closing this window will mean losing any changes you may have performed.')) {
  60          checkWindowClose(false);
  61          window.close();
  62      }
  63  }
  64  
  65  function isWhitespace(s)
  66  {
  67      var whitespace = " \t\n\r";
  68  
  69      if (s.length == 0) {
  70          // empty field!
  71          return true;
  72      } else {
  73          // check for whitespace now!
  74          for (var z = 0; z < s.length; z++) {
  75              // Check that current character isn't whitespace.
  76              var c = s.charAt(z);
  77              if (whitespace.indexOf(c) == -1) return false;
  78          }
  79          return true;
  80      }
  81  }
  82  
  83  function isEmail(s)
  84  {
  85      // email text field.
  86      var sLength = s.length;
  87      var denied_chars = new Array(" ", "\n", "\t", "\r", "%", "$", "#", "!", "~", "`", "^", "&", "*", "(", ")", "=", "{", "}", "[", "]", ",", ";", ":", "'", "\"", "?", "<", ">", "/", "\\", "|");
  88  
  89      // look for @
  90      if (s.indexOf("@") == -1) return false;
  91  
  92      // look for more than one @ sign
  93      if (s.indexOf("@") != s.lastIndexOf("@")) return false;
  94  
  95      // look for any special character
  96      for (var z = 0; z < denied_chars.length; z++) {
  97          if (s.indexOf(denied_chars[z]) != -1) return false;
  98      }
  99  
 100      // look for a dot, but also allow for a user@localhost address
 101      if ((s.indexOf(".") == -1) && (s.substring(s.lastIndexOf('@'), s.length) != '@localhost')) {
 102          return false;
 103      }
 104  
 105      // no two dots alongside each other
 106      if (s.indexOf("..") != -1) return false;
 107  
 108      // you can't have and @ and a dot
 109      if (s.indexOf("@.") != -1) return false;
 110  
 111      // the last character cannot be a .
 112      if ((s.substring(s.lastIndexOf('@'), s.length) != '@localhost.') && (
 113              (s.charAt(sLength-1) == ".") ||
 114              (s.charAt(sLength-1) == "_"))) {
 115          return false;
 116      }
 117  
 118      return true;
 119  }
 120  
 121  function hasDeniedChars(s)
 122  {
 123      var denied_chars = new Array(" ", "\n", "\t", "\r", "%", "$", "#", "!", "~", "`", "^", "&", "*", "(", ")", "=", "+", "{", "}", "[", "]", ",", ";", ":", "'", "\"", "?", "<", ">", "/", "\\", "|");
 124  
 125      for (var z = 0; z < denied_chars.length; z++) {
 126          if (s.indexOf(denied_chars[z]) != -1) return true;
 127          // checking for any non-ascii character
 128          if (s.charCodeAt(z) > 128) return true;
 129      }
 130  
 131      return false;
 132  }
 133  
 134  function hasOneSelected(f, field_name)
 135  {
 136      for (var i = 0; i < f.elements.length; i++) {
 137          if (f.elements[i].name == field_name) {
 138              var multi = f.elements[i];
 139              for (var y = 0; y < multi.options.length; y++) {
 140                  if (multi.options[y].selected) {
 141                      return true;
 142                  }
 143              }
 144          }
 145      }
 146      return false;
 147  }
 148  
 149  function hasSelected(field, value)
 150  {
 151      return field.options[field.selectedIndex].value == value;
 152  }
 153  
 154  function hasOneChecked(f, field_name)
 155  {
 156      var found = 0;
 157      for (var i = 0; i < f.elements.length; i++) {
 158          if ((f.elements[i].name == field_name) && (f.elements[i].checked)) {
 159              found = 1;
 160          }
 161      }
 162      if (found == 0) {
 163          return false;
 164      } else {
 165          return true;
 166      }
 167  }
 168  
 169  function isNumberOnly(s)
 170  {
 171      var check = parseFloat(s).toString();
 172      if ((s.length == check.length) && (check != "NaN")) {
 173          return true;
 174      } else {
 175          return false;
 176      }
 177  }
 178  
 179  function isDigit(c)
 180  {
 181      return ((c >= "0") && (c <= "9"));
 182  }
 183  
 184  function isFloat(s)
 185  {
 186      if (isWhitespace(s)) {
 187          return false;
 188      }
 189  
 190      var seenDecimalPoint = false;
 191      if (s == '.') {
 192          return false;
 193      }
 194      // Search through string's characters one by one
 195      // until we find a non-numeric character.
 196      // When we do, return false; if we don't, return true.
 197      for (var i = 0; i < s.length; i++) {
 198          // Check that current character is number.
 199          var c = s.charAt(i);
 200          if ((c == '.') && !seenDecimalPoint) {
 201              seenDecimalPoint = true;
 202          } else if (!isDigit(c)) {
 203              return false;
 204          }
 205      }
 206  
 207      // All characters are numbers.
 208      return true;
 209  }
 210  
 211  function startsWith(s, substr)
 212  {
 213      if (s.indexOf(substr) == 0) {
 214          return true;
 215      } else {
 216          return false;
 217      }
 218  }
 219  
 220  function errorDetails(f, field_name, show)
 221  {
 222      var field = getFormElement(f, field_name);
 223      var icon = getPageElement('error_icon_' + field_name);
 224      if (icon == null) {
 225          return false;
 226      }
 227      if (show) {
 228          field.style.backgroundColor = '#FF9999';
 229          icon.style.visibility = 'visible';
 230          icon.width = 14;
 231          icon.height = 14;
 232      } else {
 233          field.style.backgroundColor = '#FFFFFF';
 234          icon.style.visibility = 'hidden';
 235          icon.width = 1;
 236          icon.height = 1;
 237      }
 238  }
 239  
 240  function checkCustomFields(f)
 241  {
 242      // requires the variable custom_fields_info to be set
 243      for (var i = 0; i < custom_fields_info.length; i++) {
 244          var info = custom_fields_info[i];
 245          var field = $('custom_field_' + info.id);
 246  
 247          if ((field != false) && (field != undefined) && (field.parentNode.parentNode.style.display == 'none')) {
 248              continue;
 249          }
 250  
 251          if (info.required == 1) {
 252              if (info.type == 'combo') {
 253                  if (getSelectedOption(f, field.name) == '-1') {
 254                      errors[errors.length] = new Option(info.title, field.name);
 255                  }
 256              } else if (info.type == 'multiple') {
 257                  if (!hasOneSelected(f, field.name)) {
 258                      errors[errors.length] = new Option(info.title, field.name);
 259                  }
 260              } else if (info.type == 'date') {
 261                  if ($('custom_field_' + info.id + '_month').selectedIndex == 0) {
 262                      errors[errors.length] = new Option(info.title + ' (Month)', 'custom_field_' + info.id + '[Month]');
 263                  }
 264                  if ($('custom_field_' + info.id + '_day').selectedIndex == 0) {
 265                      errors[errors.length] = new Option(info.title + ' (Day)', 'custom_field_' + info.id + '[Day]');
 266                  }
 267                  if ($('custom_field_' + info.id + '_year').selectedIndex == 0) {
 268                      errors[errors.length] = new Option(info.title + ' (Year)', 'custom_field_' + info.id + '[Year]');
 269                  }
 270              } else {
 271                  if (isWhitespace(field.value)) {
 272                      errors[errors.length] = new Option(info.title, field.name);
 273                  }
 274              }
 275          }
 276          if (info.validation_js != '') {
 277              eval("validation_result = " + info.validation_js + '()');
 278              if (validation_result != true) {
 279                  errors_extra[errors_extra.length] = new Option(info.title + ': ' + validation_result, field.name);
 280              }
 281          } else {
 282              if (info.type == 'integer') {
 283                  if ((!isWhitespace(field.value)) && (!isNumberOnly(field.value))) {
 284                      errors_extra[errors_extra.length] = new Option(info.title + ': This field can only contain numbers', field.name);
 285                  }
 286              }
 287          }
 288  
 289      }
 290  }
 291  
 292  function checkErrorCondition(e, form_name, field_name, old_onchange)
 293  {
 294      var f = getForm(form_name);
 295      var field = getFormElement(f, field_name);
 296      if ((field.type == 'text') || (field.type == 'textarea') || (field.type == 'password')) {
 297          if (!isWhitespace(field.value)) {
 298              errorDetails(f, field_name, false);
 299              if (old_onchange != false) {
 300                  field.onchange = old_onchange;
 301                  eval('trash = ' + old_onchange + '(e)');
 302              }
 303          }
 304      } else if (field.type == 'select-one') {
 305          if (getSelectedOption(f, field_name) != '-1') {
 306              errorDetails(f, field_name, false);
 307              if (old_onchange != false) {
 308                  field.onchange = old_onchange;
 309                  eval('trash = ' + old_onchange + '(e)');
 310              }
 311          }
 312      } else if (field.type == 'select-multiple') {
 313          if (hasOneSelected(f, field_name)) {
 314              errorDetails(f, field_name, false);
 315              if (old_onchange != false) {
 316                  field.onchange = old_onchange;
 317                  eval('trash = ' + old_onchange + '(e)');
 318              }
 319          }
 320      }
 321  }
 322  
 323  function selectField(f, field_name, old_onchange)
 324  {
 325      for (var i = 0; i < f.elements.length; i++) {
 326          if (f.elements[i].name == field_name) {
 327              if (f.elements[i].type != 'hidden') {
 328                  f.elements[i].focus();
 329              }
 330              errorDetails(f, field_name, true);
 331              if (isWhitespace(f.name)) {
 332                  return false;
 333              }
 334              f.elements[i].onchange = new Function('e', 'checkErrorCondition(e, \'' + f.name + '\', \'' + field_name + '\', ' + old_onchange + ');');
 335              if (f.elements[i].select) {
 336                  f.elements[i].select();
 337              }
 338          }
 339      }
 340  }
 341  
 342  function getSelectedOption(f, field_name)
 343  {
 344      for (var i = 0; i < f.elements.length; i++) {
 345          if (f.elements[i].name == field_name) {
 346              if (f.elements[i].options.length > 0) {
 347                  if (f.elements[i].selectedIndex == -1) {
 348                      return -1;
 349                  }
 350                  return f.elements[i].options[f.elements[i].selectedIndex].value;
 351              } else {
 352                  return -1;
 353              }
 354          }
 355      }
 356  }
 357  
 358  function getSelectedOptionObject(f, field_name)
 359  {
 360      for (var i = 0; i < f.elements.length; i++) {
 361          if (f.elements[i].name == field_name) {
 362              return f.elements[i].options[f.elements[i].selectedIndex];
 363          }
 364      }
 365  }
 366  
 367  var errors = null;
 368  var errors_extra = null;
 369  function checkFormSubmission(f, callback_func)
 370  {
 371      errors = new Array();
 372      errors_extra = new Array();
 373      eval(callback_func + '(f);');
 374      if (errors.length > 0) {
 375          // loop through all of the broken fields and select them
 376          var fields = '';
 377          for (var i = 0; i < errors.length; i++) {
 378              if (getFormElement(f, errors[i].value).onchange != undefined) {
 379                  old_onchange = getFormElement(f, errors[i].value).onchange;
 380              } else {
 381                  old_onchange = false;
 382              }
 383              selectField(f, errors[i].value, old_onchange);
 384              fields += '- ' + errors[i].text + "\n";
 385          }
 386          // show a big alert box with the missing information
 387          alert("The following required fields need to be filled out:\n\n" + fields + "\nPlease complete the form and try again.");
 388          return false;
 389      } else if (errors_extra.length > 0) {
 390          // loop through all of the broken fields and select them
 391          var fields = '';
 392          for (var i = 0; i < errors_extra.length; i++) {
 393              if (getFormElement(f, errors_extra[i].value).onchange != undefined) {
 394                  old_onchange = getFormElement(f, errors_extra[i].value).onchange;
 395              } else {
 396                  old_onchange = false;
 397              }
 398              selectField(f, errors_extra[i].value, old_onchange);
 399              fields += '- ' + errors_extra[i].text + "\n";
 400          }
 401          // show a big alert box with the missing information
 402          alert("The following fields have errors that need to be resolved:\n\n" + fields + "\nPlease resolve these errors and try again.");
 403          return false;
 404      } else {
 405          return true;
 406      }
 407  }


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7