4 Replies Latest reply on Oct 2, 2007 2:01 AM by Mxmler

    Custom String Validator

    Mxmler
      Hi all,

      My Case:
      The TextInput String Validator should only accept a-z A-Z characters [No numbers, No special characters like @ + - / * )(?!... ]
      and if the user cross this rule, then the highlighted red border would appear.

      I managed to show the warning red border for the numbers but not for the special characters. I need your help to find away
      to solve this problem.

      Here this Custom String Validator::

      package
      {
      import mx.validators.Validator;
      import mx.validators.ValidationResult;

      public class CustomStringValidator extends Validator {

      // Define Array for the return value of doValidation().
      private var results:Array;

      // Constructor.
      public function CustomStringValidator() {
      // Call base class constructor.
      super();
      }

      // Define the doValidation() method.
      override protected function doValidation(value:Object):Array {

      // Clear results Array.
      results = [];
      var expression: RegExp = new RegExp();

      // Call base class doValidation().
      results = super.doValidation(value);
      // Return if there are errors.
      if (results.length > 0)
      return results;


      // If input value contains no value,
      // Issue a validation error.
      if (!value)
      {
      results.push(new ValidationResult(true, null, "NaN", "This field is required."));
      return results;
      }

      // If input value contains a number, issue a validation error.
      expression = /\d/;

      if (value.toString().search(expression) >= 0)
      {
      results.push(new ValidationResult(true, null, "NaN", "Numbers not allowed!"));
      return results;
      }

      return results;
      }
      }
      }
        • 1. Re: Custom String Validator
          dimival Level 1
          Are your validating the text on a TextInput or TextArea component? If that's the case you could restrict those components so they will only accept a-zA-Z characters, that way you wouldn't need the validator
          • 2. Re: Custom String Validator
            Mxmler Level 1
            I know that, but I would like to show it as (red border) warning rather than restrict it.
            so, the question here, how can I detect those special characters: + - / * ? ! & () ...
            • 3. Re: Custom String Validator
              Gregory Lafrance Level 6
              This works:

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="includes.*" layout="absolute">
              <mx:Script>
              <![CDATA[
              import includes.CustomStringValidator;
              private var v:CustomStringValidator = new CustomStringValidator();

              private function performValidation():void {
              // Set the listener property to the component
              // used to display validation errors.
              v.listener=txt;
              v.validate(txt.text);
              }
              ]]>
              </mx:Script>
              <mx:Panel>
              <mx:TextInput id="txt"/>
              <mx:Button label="Validate" id="btn" click="performValidation()"/>
              </mx:Panel>
              </mx:Application>

              package includes
              {

              import mx.validators.Validator;
              import mx.validators.ValidationResult;

              public class CustomStringValidator extends Validator {

              // Define Array for the return value of doValidation().
              private var results:Array;

              // Constructor.
              public function CustomStringValidator() {
              // Call base class constructor.
              super();
              }

              // Define the doValidation() method.
              override protected function doValidation(value:Object):Array {

              // Clear results Array.
              results = [];
              var expression1: RegExp = new RegExp();
              var expression2: RegExp = new RegExp();

              // Call base class doValidation().
              results = super.doValidation(value);

              // Return if there are errors.
              if (results.length > 0)
              return results;


              // If input value contains no value,
              // Issue a validation error.
              if (!value)
              {
              results.push(new ValidationResult(true, null, "NaN", "This field is required."));
              return results;
              }

              // If input value contains a number, issue a validation error.
              expression1 = /\d/;
              // If input value contains a special character, issue a validation error.
              expression2 = /([a-z]+)|([A-Z]+)/

              if (value.toString().search(expression1) >= 0)
              {
              results.push(new ValidationResult(true, null, "NaN", "Numbers not allowed!"));
              return results;
              }
              if ((value.toString().search(expression1) < 0) && (value.toString().search(expression2) < 0))
              {
              results.push(new ValidationResult(true, null, "NaN", "Only letter characters allowed!"));
              return results;
              }

              return results;
              }
              }
              }


              • 4. Re: Custom String Validator
                Mxmler Level 1
                Thanks Greg for your solution, I have found another one way by using
                fromCharCode & charCodeAt

                Thanks again