Generate random codes in Client-Side of GWT

喜欢而已 提交于 2019-12-04 23:23:34

问题


I have created a java gwt application in which I want to verify user's email address from client side, is there any way to generate random 5 character code on client side?

Any sort of help will be appreciated.


回答1:


Why don't you test with Java Math.random() .You can simply get by it.

Here is useful formula for generating random numbers

(int)(Math.random() * (max - min) + min)

So , you can generate 5 random numbers as like...

String randomCodes = String.valueOf((int) (Math.random() * (99999 - 1) + 1));
    while (randomCodes.length() < 5) {
            randomCodes = "0" + randomCodes;
        }



回答2:


Something like this?

StringBuilder sb = new StringBuilder();
Random random = new Random();

for (int i=0;i<5;i++) {
    sb.append('a'+random.nextInt(26));
}
String code = sb.toString();



回答3:


You can use the RandomStringUtils from the Apache Commons project,

RandomStringUtils.randomAlphabetic(5);



来源:https://stackoverflow.com/questions/20837579/generate-random-codes-in-client-side-of-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!