Validation in swt text
Asked Answered
F

6

6

I would like to validate numbers input in text box.

I want the user to input only integer, decimal in the box between a maximum and minimum values.

How can I make sure of this?

Thank you.

Friel answered 13/3, 2012 at 6:56 Comment(0)
L
11

Use a VerifyListener as this will handle paste, backspace, replace.....

E.g.

text.addVerifyListener(new VerifyListener() {
  @Override
  public void verifyText(VerifyEvent e) {
    final String oldS = text.getText();
    final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);

    try {
      BigDecimal bd = new BigDecimal(newS);
      // value is decimal
      // Test value range
    } catch (final NumberFormatException numberFormatException) {
      // value is not decimal
      e.doit = false;
    }
  }
});
Lactase answered 13/3, 2012 at 20:36 Comment(1)
Partial expressions like "4e-" should be allowed to be able to enter "4e-3"Hannigan
F
3

You can register a ModifyListener with the text control and use it to validate the number.

    txt.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent event) {
            String txt = ((Text) event.getSource()).getText();
            try {
                int num = Integer.parseInt(txt);
                // Checks on num
            } catch (NumberFormatException e) {
                // Show error
            }
        }
    });

You could also use addVerifyListener to prevent certain characters being entered. In the event passed into that method there is a "doit" field. If you set that to false it prevents the current edit.

Freese answered 13/3, 2012 at 10:41 Comment(0)
B
3

Integer validation in SWT

text = new Text(this, SWT.BORDER);
text.setLayoutData(gridData);
s=text.getText();
this.setLayout(new GridLayout());
text.addListener(SWT.Verify, new Listener() {
   public void handleEvent(Event e) {
      String string = e.text;
      char[] chars = new char[string.length()];
      string.getChars(0, chars.length, chars, 0);
      for (int i = 0; i < chars.length; i++) {
         if (!('0' <= chars[i] && chars[i] <= '9')) {
            e.doit = false;
            return;
         }
      }
   }
});
Burtonburty answered 29/5, 2012 at 9:6 Comment(0)
H
0
  • If you only want to allow Integer values you could use a Spinner instead of a text field.

  • For the validation of Double values you have to consider that characters like E may be included in Doubles and that partial input like "4e-" should be valid while typing. Such partial expressions will give a NumberFormatException for Double.parseDouble(partialExpression)

  • Also see my answer at following related question: How to set a Mask to a SWT Text to only allow Decimals

Hannigan answered 17/3, 2016 at 18:12 Comment(0)
M
0

1.create a utill class implement verify listener

2.override verifytext method

3.implement your logic

4.create a object of utill class where you want to use verify listener (on text)

5.text.addverifylistener(utillclassobject)

Example :- 1. utill class:-

public class UtillVerifyListener implements VerifyListener {

@Override
public void verifyText(VerifyEvent e) {

    // Get the source widget
    Text source = (Text) e.getSource();

    // Get the text
    final String oldS = source.getText();
    final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);

    try {
        BigDecimal bd = new BigDecimal(newS);
        // value is decimal
        // Test value range
    } catch (final NumberFormatException numberFormatException) {
        // value is not decimal
        e.doit = false;
    }
}

2.use of verifylistener in other class

public class TestVerifyListenerOne {
public void createContents(Composite parent){

    UtillVerifyListener listener=new UtillVerifyListener();
    textOne = new Text(composite, SWT.BORDER);
    textOne .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    textOne .addVerifyListener(listener)

    textTwo  = new Text(composite, SWT.BORDER);
    textTwo  .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    textTwo  .addVerifyListener(listener);

} }

Makings answered 31/5, 2017 at 5:45 Comment(0)
M
0
text.addVerifyListener(new VerifyListener() {
    @Override
    public void verifyText(VerifyEvent e) {
        Text text = (Text) e.getSource();

        final String oldS = text.getText();
        String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);

        boolean isValid = true;

        try {

            if(! "".equals(newS)){
                Float.parseFloat(newS);
            }

        } catch (NumberFormatException ex) {
            isValid = false;
        }

        e.doit = isValid;
    }
});
Maelstrom answered 24/9, 2017 at 2:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.