Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:10:00+00:00 2026-06-12T09:10:00+00:00

How can add validation to an SWT widget? E.g. Text ? I tried both

  • 0

How can add validation to an SWT widget? E.g. Text?
I tried both of the following (found online):

txtPort.addListener(SWT.Verify,new Listener() {  
  @Override  
  public void handleEvent(Event event) {  
      String port = ((Text)event.widget).getText();  
      try{  
          int portNum = Integer.valueOf(port);  
          if(portNum <0 || portNum > 65535){  
                event.doit = false;  
         }  
      }  
      catch(Exception ex){  
         event.doit = false;  
      }                 
   }  
});  

Also:

txtPort.addVerifyListener(new VerifyListener() {  
   @Override  
   public void verifyText(VerifyEvent e) {  
     String port = ((Text)e.widget).getText();  
      try{  
         int portNum = Integer.valueOf(port);  
         if(portNum <0 || portNum > 65535){  
             e.doit = false;  
          }  
      }  
      catch(Exception ex){  
          e.doit = false;  
      }  
   }  
});     

If I add a character, the cursor stucks and I can not even delete it.
Even if I just delete everything the first time, the cursor also stucks and I can not write anything else.
What am I messing up here? How am I supposed to do the validation of a Text?
In this case I only want to accept a number serving as port.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:10:00+00:00Added an answer on June 12, 2026 at 9:10 am

    The VerifyListener you are creating will be called before any text has actually been entered. You a currently checking the text that has already been entered to see if the value is valid, but it will never be valid because no text has yet been entered.

    Try reading the value of e.text to see if it’s an integer and use the e.start and e.end properties along with the the getText() you have now to see if the overall new value will be between 0 and 65535.

    An easier solution may be to create a ModifyListener that only enables a submit button when the Text widget’s text contains a valid port number.

    You can try something similar to this:

    txtPort.addVerifyListener(new VerifyListener() {  
        @Override  
        public void verifyText(VerifyEvent e) {
            /* Notice how we combine the old and new below */
            String currentText = ((Text)e.widget).getText();
            String port =  currentText.substring(0, e.start) + e.text + currentText.substring(e.end);
            try{  
                int portNum = Integer.valueOf(port);  
                if(portNum <0 || portNum > 65535){  
                    e.doit = false;  
                }  
            }  
            catch(NumberFormatException ex){  
                if(!port.equals(""))
                    e.doit = false;  
            }  
        }  
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found where you can add jquery validation rules dynamically like this: $(#ParentAdNumber).rules(add, {
I am using the jquery validation plugin from: http://bassistance.de/jquery-plugins/jquery-plugin-validation/ How can I add a
I am working in javascript. I want to add validation on my text field
I need to add validation on a before_validation model filter. How can I do
Is there any way I can add a validation to the user model such
I have a form (new poll form) in which I can add multiple items
in MVC3 you can add validation to models to check if properties match like
I have a Friend model: user_id, friend_id, status How can I add a validation
How can I perform a text validation on a textbox in form written in
In WF4 custom activities, I understand you can add warning of validation error by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.