techie
techie

Reputation: 467

Validation for textfield to allow upCase as first letter-Blackberry

How to allow first letter of an input in textfield to be caps.Or is it possible to change on textfield text change event.Also i want to avoid having a space as first character.(Though it may be used in middle of text)

Here is the code for textfield validation for testing for other parameters

    TextField1 = new TextField("\n Customer Name: ",null)
     {
            protected boolean keyChar(char ch, int status, int time) 
            {
            if (CharacterUtilities.isLetter(ch) || (ch == Characters.BACKSPACE || (ch == Characters.SPACE))) 
            {
            return super.keyChar(ch, status, time);
            }
           return true;
            }
        };
     add(TextField1);

Please guide.Thanks

Reedit:

      AutoTextEditField auto = new AutoTextEditField("Name: ",EditField.NO_NEWLINE | Field.EDITABLE | AutoTextEditField.NON_SPELLCHECKABLE);

     add(auto);

Reedit 2: Using below code prevent any spaces from being typed(at start or between).Also still there is no validation for Caps as first letter being done. Please see this link for how I got the below idea

BasicEditField bf = new BasicEditField("BasicEditField: ","", 10,EditField.FILTER_UPPERCASE); 
     class AlphaTextFilter extends TextFilter
    {
      public char convert(char c, int status) 
      {
      if (!validate(c))
           return 0;
           return c;
      }

       public boolean validate(char c) 
       {
         return CharacterUtilities.isLetter(c);
       }
    }
    bf.setFilter(new AlphaTextFilter());
    add(bf);

Upvotes: 0

Views: 454

Answers (1)

Eugen Martynov
Eugen Martynov

Reputation: 20130

Use AutoTextEditField instead of TextField with the following flags: EditField.NO_NEWLINE | Field.EDITABLE | AutoTextEditField.NON_SPELLCHECKABLE.

Don't afraid spaces in front of the real value. Just call trim() for final value.

Upvotes: 1

Related Questions