commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rlel...@apache.org
Subject cvs commit: jakarta-commons/validator/src/javascript/org/apache/commons/validator/javascript validateShort.js validateInteger.js validateFloat.js validateByte.js
Date Sun, 18 May 2003 21:34:41 GMT
rleland     2003/05/18 14:34:41

  Added:       validator/src/javascript/org/apache/commons/validator/javascript
                        validateShort.js validateInteger.js
                        validateFloat.js validateByte.js
  Log:
  Add Javascript validations for Short/Integer/Byte/Float
  
  Revision  Changes    Path
  1.1                  jakarta-commons/validator/src/javascript/org/apache/commons/validator/javascript/validateShort.js
  
  Index: validateShort.js
  ===================================================================
  
      function validateShort(form) {
          var bValid = true;
          var focusField = null;
          var i = 0;
          var fields = new Array();
          oShort = new ShortValidations();
          for (x in oShort) {
              var field = form[oShort[x][0]];
  
              if (field.type == 'text' ||
                  field.type == 'textarea' ||
                  field.type == 'select-one' ||
                  field.type == 'radio') {
  
                  var value = '';
                  // get field's value
                  if (field.type == "select-one") {
                      var si = field.selectedIndex;
                      if (si >= 0) {
                          value = field.options[si].value;
                      }
                  } else {
                      value = field.value;
                  }
  
                  if (value.length > 0) {
                      if (!isAllDigits(value)) {
                          bValid = false;
                          if (i == 0) {
                              focusField = field;
                          }
                          fields[i++] = oShort[x][1];
  
                      } else {
  
                          var iValue = parseInt(value);
                          if (isNaN(iValue) || !(iValue >= -32768 && iValue <=
32767)) {
                              if (i == 0) {
                                  focusField = field;
                              }
                              fields[i++] = oShort[x][1];
                              bValid = false;
                          }
                     }
                 }
              }
          }
          if (fields.length > 0) {
             focusField.focus();
             alert(fields.join('\n'));
          }
          return bValid;
      }
  
  
  1.1                  jakarta-commons/validator/src/javascript/org/apache/commons/validator/javascript/validateInteger.js
  
  Index: validateInteger.js
  ===================================================================
  
      function validateInteger(form) {
          var bValid = true;
          var focusField = null;
          var i = 0;
          var fields = new Array();
          oInteger = new IntegerValidations();
          for (x in oInteger) {
              var field = form[oInteger[x][0]];
  
              if (field.type == 'text' ||
                  field.type == 'textarea' ||
                  field.type == 'select-one' ||
                  field.type == 'radio') {
  
                  var value = '';
                  // get field's value
                  if (field.type == "select-one") {
                      var si = field.selectedIndex;
                      if (si >= 0) {
                          value = field.options[si].value;
                      }
                  } else {
                      value = field.value;
                  }
  
                  if (value.length > 0) {
  
                      if (!isAllDigits(value)) {
                          bValid = false;
                          if (i == 0) {
                              focusField = field;
                          }
                          fields[i++] = oInteger[x][1];
  
                      } else {
                          var iValue = parseInt(value);
                          if (isNaN(iValue) || !(iValue >= -2147483648 && iValue
<= 2147483647)) {
                              if (i == 0) {
                                  focusField = field;
                              }
                              fields[i++] = oInteger[x][1];
                              bValid = false;
                         }
                     }
                 }
              }
          }
          if (fields.length > 0) {
             focusField.focus();
             alert(fields.join('\n'));
          }
          return bValid;
      }
  
      function isAllDigits(argvalue) {
          argvalue = argvalue.toString();
          var validChars = "0123456789";
          var startFrom = 0;
          if (argvalue.substring(0, 2) == "0x") {
             validChars = "0123456789abcdefABCDEF";
             startFrom = 2;
          } else if (argvalue.charAt(0) == "0") {
             validChars = "01234567";
             startFrom = 1;
          } else if (argvalue.charAt(0) == "-") {
              startFrom = 1;
          }
  
          for (var n = startFrom; n < argvalue.length; n++) {
              if (validChars.indexOf(argvalue.substring(n, n+1)) == -1) return false;
          }
          return true;
      }
  
  
  1.1                  jakarta-commons/validator/src/javascript/org/apache/commons/validator/javascript/validateFloat.js
  
  Index: validateFloat.js
  ===================================================================
  
      function validateFloat(form) {
          var bValid = true;
          var focusField = null;
          var i = 0;
          var fields = new Array();
          oFloat = new FloatValidations();
          for (x in oFloat) {
          	var field = form[oFloat[x][0]];
          	
              if (field.type == 'text' ||
                  field.type == 'textarea' ||
                  field.type == 'select-one' ||
                  field.type == 'radio') {
          
              	var value = '';
                  // get field's value
                  if (field.type == "select-one") {
                      var si = field.selectedIndex;
                      if (si >= 0) {
                          value = field.options[si].value;
                      }
                  } else {
                      value = field.value;
                  }
          
                  if (value.length > 0) {
                      // remove '.' before checking digits
                      var tempArray = value.split('.');
                      var joinedString= tempArray.join('');
   
                      if (!isAllDigits(joinedString)) {
                          bValid = false;
                          if (i == 0) {
                              focusField = field;
                          }
                          fields[i++] = oFloat[x][1];
  
                      } else {
  	                var iValue = parseFloat(value);
  	                if (isNaN(iValue)) {
  	                    if (i == 0) {
  	                        focusField = field;
  	                    }
  	                    fields[i++] = oFloat[x][1];
  	                    bValid = false;
  	                }
                      }
                  }
              }
          }
          if (fields.length > 0) {
             focusField.focus();
             alert(fields.join('\n'));
          }
          return bValid;
      }
  
  
  1.1                  jakarta-commons/validator/src/javascript/org/apache/commons/validator/javascript/validateByte.js
  
  Index: validateByte.js
  ===================================================================
  
      function validateByte(form) {
          var bValid = true;
          var focusField = null;
          var i = 0;
          var fields = new Array();
          oByte = new ByteValidations();
          for (x in oByte) {
              var field = form[oByte[x][0]];
  
              if (field.type == 'text' ||
                  field.type == 'textarea' ||
                  field.type == 'select-one' ||
                  field.type == 'radio') {
  
                  var value = '';
                  // get field's value
                  if (field.type == "select-one") {
                      var si = field.selectedIndex;
                      if (si >= 0) {
                          value = field.options[si].value;
                      }
                  } else {
                      value = field.value;
                  }
  
                  if (value.length > 0) {
                      if (!isAllDigits(value)) {
                          bValid = false;
                          if (i == 0) {
                              focusField = field;
                          }
                          fields[i++] = oByte[x][1];
  
                      } else {
  
                          var iValue = parseInt(value);
                          if (isNaN(iValue) || !(iValue >= -128 && iValue <=
127)) {
                              if (i == 0) {
                                  focusField = field;
                              }
                              fields[i++] = oByte[x][1];
                              bValid = false;
                          }
                      }
                  }
  
              }
          }
          if (fields.length > 0) {
             focusField.focus();
             alert(fields.join('\n'));
          }
          return bValid;
      }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message