简体   繁体   English

如何在Java ME中验证TextField中的输入值

[英]How to validate input value in a TextField in Java ME

I am writing a simple SRMS, and I need to validate the input from the user if it matches some criteria depending on the field, eg an email field or a phone field. 我正在编写一个简单的SRMS,如果它符合某些标准,我需要验证用户的输入,具体取决于字段,例如电子邮件字段或电话字段。 The app is to run in a featured phone and so I am using the Java ME SDK with a virtual machine for testing. 该应用程序将在特色手机中运行,因此我使用Java ME SDK和虚拟机进行测试。

What is the best way to do so, what would be the best way to validate the input and if the input does not meet some criteria, should the user be notified or the value she has entered to be set to null again. 这样做的最佳方法是什么,验证输入的最佳方法是什么,如果输入不符合某些条件,应该通知用户还是将输入的null再次设置为null

public void name() {
    boolean nameValid = false;
    display = Display.getDisplay(this);
    nameForm = new Form("Student Record Management (1/4");
    TextField firstName = new TextField("First Name(s)", "", 20, TextField.ANY);
    TextField lastName = new TextField("Last Name", "", 20, TextField.ANY);
    TextField personNumber = new TextField("Person Number", "", 10, TextField.NUMERIC);
    back = new Command("BACK", Command.BACK, 1);
    next = new Command("Continue", Command.ITEM, 2);

    nameForm.append(firstName);
    nameForm.append(lastName);
    nameForm.append(personNumber);
    nameForm.addCommand(back);
    nameForm.addCommand(next);
    nameForm.setItemStateListener(this);
    nameForm.setCommandListener(this);
    display.setCurrent(nameForm);

    if (firstName.toString().length() > 0) {
        nameValid = true;
    }
}

The person who started the code has implemented the CommandListener and ItestStateListener . 启动代码的人已实现CommandListenerItestStateListener

I am not sure what is the second one does and it has an abstract method to be filled which is called itemStateChanged(Item item) am I supposed to check for changes and validate in here ? 我不确定第二个做了什么,它有一个抽象的方法被填充,称为itemStateChanged(Item item)我应该检查更改并在此处验证?

public static boolean validateEmailID(String email) {
email = email.trim();
String reverse = new StringBuffer(email).reverse().toString();
if (email == null || email.length() == 0 || email.indexOf("@") == -1) {
    return false;
}
int emailLength = email.length();
int atPosition = email.indexOf("@");
int atDot = reverse.indexOf(".");

String beforeAt = email.substring(0, atPosition);
String afterAt = email.substring(atPosition + 1, emailLength);

if (beforeAt.length() == 0 || afterAt.length() == 0) {
    return false;
}
for (int i = 0; email.length() - 1 > i; i++) {
    char i1 = email.charAt(i);
    char i2 = email.charAt(i + 1);
    if (i1 == '.' && i2 == '.') {
        return false;
    }
}
if (email.charAt(atPosition - 1) == '.' || email.charAt(0) == '.' || email.charAt(atPosition + 1) == '.' || afterAt.indexOf("@") != -1 || atDot < 2) {
    return false;
}

return true;

} }

The ItemStateListener notifies the application of changes in Form items. ItemStateListener通知应用程序表单项中的更改。 The item itemStateChanged(Item item) method is called when an item in your form is changed by the user or when Item.notifyStateChanged() is called in an Item. 用户更改表单中的项目或在Item中调用Item.notifyStateChanged(),将调用itemStateChanged(Item item)项。 The argument is the Item (Textfield, DateField, ect) that changed value. 参数是更改值的Item(Textfield,DateField,ect)。

I would recommend that you call your validation method inside both the CommandAction and ItemStateListener. 我建议您在CommandAction和ItemStateListener中调用验证方法。 In the itemStateChanged only the current Item (the one received in the argument) should be checked. 在itemStateChanged中,只应检查当前Item(在参数中接收的那个)。 In the CommandAction every field should be checked. 在CommandAction中,应检查每个字段。 This way every Item is validated in every situation. 这样每个项目都可以在任何情况下进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM