开发者

Test if two first chars typed in are alphanumeric - no regex

开发者 https://www.devze.com 2023-03-11 06:03 出处:网络
I have following code that needs something smart to deal with typed in chars and detection: private final MultiWordSuggestOracle mySuggestions = new MultiWordSuggestOracle();

I have following code that needs something smart to deal with typed in chars and detection:

private final MultiWordSuggestOracle mySuggestions = new MultiWordSuggestOracle();
private final Set<String> mySuggestionsData = new HashSet<String>();

@UiHandler("suggestBox")
public void onKeyPress(KeyDownEvent event) {
    if (Character.isLetterOrDigit(event.getCharCode())) {
        char[] text = suggestBox.getText().trim().toCharArray();
        if (text.length != 1) return;

        for (char ch : text) {
            if (!Character.isLetterOrDigit(ch)) {
                return;
            }
        }
        //load data from server into mySug开发者_JAVA百科gestionsData
    }     
}

The question has 3 parts:

  1. How do you test pressed key against alphanumeric chars. Keep in mind this is GWT so I would rather not use regex ( but if there is no other option ...).

  2. What is the best way to detect the length of text typed into the SuggestBox?

  3. Is KeyDownEven the best choise? And why is it triggered twice when any key is pressed?


Instead of handling events, you should make your own SuggestOracle (possible wrapping a MultiSuggestOracle used as an internal cache) and check the query's length and "pattern" there to decide whether to call the server or not (and then give an empty list of suggestions as the response, or maybe a single suggestion being the exact query).

As a side note, I don't understand why you don't want to use a regex; either using the java.lang.String methods taking a regex as a String, or the com.google.gwt.regexp.shared.RegExp class.


1. I'd use KeyPressHandler instead of Up/Down handler.

As far as I understand, you are more interested to get what the user has typed but not the key that was actually pressed on the keyboard. Also, you can use Character.isDigit(c) and Character.isLetter(c) since KeyPressEvent.getCharCode() will return char (c).

2. Likely you want to check text length at some point (e.g. when user presses Enter), then

// handler block
if (c == KeyCodes.KEY_ENTER) {
    int length = ((SuggestBox) event.getSource()).getText().length();
    // take an action depending on length
}
// handler block continued

should fit.

3. See [1] and perhaps it's browser specific.

Edit: [1],[2] and [3] combined (using KeyUpHandler):

private static final int THREASHOLD = 2;
private String phrase = "";
...
    searchBox.addKeyUpHandler(new KeyUpHandler() {
        @Override
        public void onKeyUp(KeyUpEvent event) {
            String text = ((SuggestBox) event.getSource()).getText();
            if (!phrase.equals(text)) {
                if (text.length() >= THREASHOLD) {
                    boolean alphanum = true;
                    for (int i = 0; i < THREASHOLD; i++) {
                        char c = text.charAt(i);
                        if (!Character.isDigit(c) && !Character.isLetter(c)) {
                            alphanum = false;
                            break;
                        }
                    }
                    if (alphanum) {
                        //RPC (won't be called twice)
                    }
                }
                phrase = text;
            }
        }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号