I had been browsing the forum to get ideas from members.
I was trying to do some validations on input entered by users through a form. Validation involve say Input fields must not empty and one must be an Integer and one must be a float.
Field must not empty works fine.
One Input must be an Integer works fine.
Problem when come to float it seem my coding does not working the way I want it.
Here is part of the validation code:
public boolean validate() {
boolean allOk=true;
if (noreg_majikan.equals("")) {
errors.put("noreg_majikan"," Perlu diisi.");
noreg_majikan="";
allOk=false;
}
if (bil_kosong.equals("")) {
errors.put("bil_kosong"," Perlu diisi.");
bil_kosong="";
allOk=false;
}else {
try {
int x = Integer.parseInt(bil_kosong);
}
catch (NumberFormatException e) {
errors.put("bil_kosong","Perlu dalam bentuk angka");
bil_kosong="";
allOk=false;
}
}
if (gaji.equals("")) {
errors.put("gaji"," Perlu diisi.");
gaji="";
allOk=false;
}else {
try {
float y = Float.parseFloat(gaji);
}
catch (NumberFormatException e) {
errors.put("gaji","Perlu dalam bentuk angka");
gaji="";
allOk=false;
}
}
return allOk;
}
For the float part if the field not entered (empty) it give the correct message. If I enter a String it give the correct message.
When I entered 9 ( or any integer) it works. But when I entered say 99.50 it give error as if I had entered a String.
I just want to know why?
Thanx