我有許多需要輸入數據的字段。這是一個酒店預訂系統,所以如果這些字段沒有填滿,它必須顯示它們是空的,並且不填充它們就不能繼續。我想要做的是從字段中獲取文本,但如果它們爲空,則必須將所有字段文本設置爲「*請填寫所有字段」或顯示消息。 我有一些代碼不工作,因爲如果字段中沒有任何內容,它就不能獲取文本。代碼如下所示:從字段中獲取文本,但如果空白,請執行此操作
this.Firstname = NameF.getText();
this.Lastname = NameL.getText();
this.Country = Countr.getText();
this.IDtype = IDTy.getText();
this.PassportNo = PassNo.getText();
this.IDNo = IDNumber.getText();
this.Addr1 = Add1.getText();
this.Addr2 = Add2.getText();
this.AreaCode = Integer.parseInt(Area.getText());
this.TelNo = Tel.getText();
this.CellNo = Cell.getText();
this.Email = Em.getText();
}
if (this.Firstname.equals("") || this.Lastname.equals("") || this.Country.equals("") || this.IDtype.equals("") || this.IDNo.equals("") || this.Addr1.equals("") || this.Addr2.equals("") || this.AreaCode == 0 || this.TelNo.equals("") || this.CellNo.equals("") || this.Email.equals("")) {
JOptionPane.showMessageDialog(null, "Please fill in all fields");
}
不知道如果我要問這一個問題,但如果有沒有這麼多||
運營商做出了一個更簡單的方法?就像if this.Firstname,this.Lastname,etc.equals("")
考慮使用JGoodies來幫助您驗證表單。 – vels4j
您可能會考慮在發生'EventListener'時進行驗證,也可以要求提供yourContainer.getComponents(),並且如果組件instanceof textfield驗證那裏 – nachokk