1 Reply Latest reply on Feb 2, 2011 2:21 AM by _spoboyle

    Custom validator for Unix directory name

    2009 Matt Level 1

      I need to write a validator that validates the TextInput for UNIX directory name. I think only letters and numbers plus "-" and "_" are allowed. Any examples?

        • 1. Re: Custom validator for Unix directory name
          _spoboyle Level 4

          http://www.cse.ohio-state.edu/sce/reference/unix/filenames.html

           

          remove the second if statement in validateFilename if you are not restircted by filename length

           

          package
          {
              import mx.validators.Validator;
             
              public class UnixFilenameValidator extends Validator
              {
                  public function UnixFilenameValidator()
                  {
                      super();
                  }
                 
                  override protected function doValidation(value:Object):Array
                  {
                      var results:Array = super.doValidation(value);
                     
                      var val:String = value ? String(value) : "";
                      if (results.length > 0 || ((val.length == 0) && !required))
                          return results;
                      else
                          return UnixFilenameValidator.validateFilename(this, value, null);
                  }
                 
                  protected static function validateFilename(validator:UnixFilenameValidator, value:Object, baseField:String)
                  {
                      var filename:String = value.toString();
                      if (filename.indexOf("/") == -1)
                      {
                          return [new ValidationResult(true, null, "NON_VALID_CHAR", "The file name should not contain '/'.")];
                      }
                      if (filename.length > 256)
                      {
                          return [new ValidationResult(true, null, "FILENAME_TOO_LONG", "The file name should not exceed 256 characters.")];
                      }
                      return [];
                  }
              }
          }

          1 person found this helpful