I have two EditText like editText1 and editText2.
I am taking input from EditText to name1 and name2.
Now I want to validate that the EditText are not empty before clicking on ok button.
For this I used:
if(editText1==NULL && editText2==NULL) {
textView.setText("please enter Names");
} else {
other code;
}
But its not validating, and with empty EditText it is showing result on clicking ok button.
I have also tried in this way:
if(name1==NULL && name2==NULL) {
textView.setText("please enter Names");
} else {
other code;
}
What to do?
Try this:
if(editText1.getText().toString().trim().equals("")
&& editText2.getText().toString().trim().equals(""))
{
textView.setText("Please enter Names");
}
else
{
// other code
}
Please use:
if(name1.compareTo("") == 0 || name2.compareTo("") == 0) {
// Your piece of code for example
Toast toast=Toast.makeText(getApplicationContext(), "ENTER NAMES", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
} else {
// Your code
}
This will work definitely for sure.
Try like this name1.equals("") in place of name1==NULL
if(name1.equals("") && name2.equals(""))
{
textView.setText("please enter Names");
}
else
{
other code;
}
if(name1.compareTo("") == 0 || name2.compareTo("") == 0)
{
// Your piece of code for example
}
you can use:
if(name.compareTo("") == 0 || name.compareTo(null) == 0) {}
This works very well for me, try this
if(editText1.getText().toString().length == 0 && editText2.getText().toString().length == 0)
{
textView.setText("Please enter names");
}
else
{
//some other actions to perform
}
if(editText1.getText().toString().length() > 0 && editText2.getText().toString().length() > 0)
{
textView.setText("Please enter Names");
}
else
{
// Other code
}
Related
I think this counts for any other language as well, but I'm using Java for Android in this example here. I'm trying to validate a form, but it has multiple inputs.
if( !anuncio.getEstado().isEmpty() ){
if( !anuncio.getCategoria().isEmpty() ){
if( !anuncio.getTitulo().isEmpty() ){
if( !valor.isEmpty() && !valor.equals("0") ){
if( !anuncio.getTelefone().isEmpty() ){
if( !anuncio.getDescricao().isEmpty() ){
saveForm();
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
}else {
showErro("please fill the entire form");
}
Is there any way or method to simplify this? Because let's say if it were 10 inputs, it would be a horrible code doing 10 ifs, you know? How do I simplify this?
In your case this would suffice:
if( !anuncio.getEstado().isEmpty()
&& !anuncio.getCategoria().isEmpty()
&& !anuncio.getTitulo().isEmpty()
&& !valor.isEmpty() && !valor.equals("0")
&& !anuncio.getTelefone().isEmpty()
&& !anuncio.getDescricao().isEmpty() ) {
safeForm();
} else {
showErro("please fill the entire form");
}
For more complex code you may i.e. want to count errors and show messages later if count > 0 etc.
PS also I believe you wanted to write "showError" not "showErro"
More elegant way to write it:
if(!containsEmptyText(anuncio.getEstado(), anuncio.getCategoria(), anuncio.getTitulo(),
valor, anuncio.getTelefone(), anuncio.getDescricao()) && !valor.equals("0") ){
saveForm();
}else
showErro("please fill the entire form");
private static boolean containsEmptyText(String... textArray){
for(String text : textArray)
if(text.isEmpty())
return true;
return false;
}
I am trying to validate some edittext. I Made 7 edittexts. Phone number ,First name , Last name , Pickup Address, Postal Code, Pickup time, Number of products. As u can see i made 7 edittexts and I want to validate them so that when i press the button to go to next page it does all the database posting and getting on that button. I've already made a method (public void addInfo) and now i am adding the validations on the View.OnClick implemented methods. Here is my Code i wrote for validations.
public void onClick(View v) {
if (v == nextbuttonjava) {
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
}
if(firstnamejava.getText().toString().length() == 0)
{
firstnamejava.setError("Whats your first name?");
}
if(lastnamejava.getText().toString().length() == 0)
{
lastnamejava.setError("Whats your last name?");
}
if(pickupaddressjava.getText().toString().length() == 0)
{
pickupaddressjava.setError("Enter your address");
}
if(postalcodejava.getText().toString().length() < 6)
{
postalcodejava.setError("Invalid postal code");
}
if(pickuptimejava.getText().toString().length() == 0)
{
pickuptimejava.setError("Enter pickuptime");
}
if(numberstuffjava.getText().toString().length() == 0) {
numberstuffjava.setError("Enter number of clothes");
}
else
{
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
}
}
I Want that every If statement should be true to make the else work so the intent works and i go to the other activity. But its not the scene here. Only the phone number works good(If i enter the phone number below 10 digits it shows the error and it doesn't run else part). My main concern is that I Have 7 edittexts and Even if one If statement goes true then the else shouldn't be running and i shouldn't go to next intent with posting incomplete data on my database.I Want that even if any of the edittexts if statement goes wrong i should get an error (eg, numberstuffjava.setError("Input Something");.
How to resolve the issue?
Try something like this:
public void onClick(View v) {
if (v == nextbuttonjava) {
boolean error = false;
if(phnumberjava.getText().toString().length() < 10)
{
error = true;
phnumberjava.setError("Invalid Phone Number");
}
if(firstnamejava.getText().toString().isEmpty())
{
error = true;
firstnamejava.setError("Whats your first name?");
}
if(lastnamejava.getText().toString().isEmpty())
{
error = true;
lastnamejava.setError("Whats your last name?");
}
if(pickupaddressjava.getText().toString().isEmpty())
{
error = true;
pickupaddressjava.setError("Enter your address");
}
if(postalcodejava.getText().toString().length() < 6)
{
error = true;
postalcodejava.setError("Invalid postal code");
}
if(pickuptimejava.getText().toString().isEmpty())
{
error = true;
pickuptimejava.setError("Enter pickuptime");
}
if(numberstuffjava.getText().toString().isEmpty())
{
error = true;
numberstuffjava.setError("Enter number of clothes");
}
if(!error)
{
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
} else {
//manage error case here
}
}
}
another option could be this:
if (v == nextbuttonjava) {
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
}
else if(firstnamejava.getText().toString().isEmpty())
{
firstnamejava.setError("Whats your first name?");
}
else if(lastnamejava.getText().toString().isEmpty())
{
lastnamejava.setError("Whats your last name?");
}
else if(pickupaddressjava.getText().toString().isEmpty())
{
pickupaddressjava.setError("Enter your address");
}
else if(postalcodejava.getText().toString().length() < 6)
{
postalcodejava.setError("Invalid postal code");
}
else if(pickuptimejava.getText().toString().isEmpty())
{
pickuptimejava.setError("Enter pickuptime");
}
else if(numberstuffjava.getText().toString().isEmpty())
{
numberstuffjava.setError("Enter number of clothes");
}
else
{
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
else
{
//manage error case here
}
}
In this way you check any case with the else if. I prefer the first solution because it's more "flexible".
Hope this helps!
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
return "Error message";
}
else {
return null;
}
return like this in each and every if condition. If the return statement is null, then all if conditions are true. else show the error message returned
Among other things, you can try the following logic. Here, you can be assured that all the error messages will be shown:
if (v == nextbuttonjava) {
boolean validated = true;
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
validated = false;
}
if(firstnamejava.getText().toString().length() == 0)
{
firstnamejava.setError("Whats your first name?");
validated = false;
}
if(lastnamejava.getText().toString().length() == 0)
{
lastnamejava.setError("Whats your last name?");
validated = false;
}
if(pickupaddressjava.getText().toString().length() == 0)
{
pickupaddressjava.setError("Enter your address");
validated = false;
}
if(postalcodejava.getText().toString().length() < 6)
{
postalcodejava.setError("Invalid postal code");
validated = false;
}
if(pickuptimejava.getText().toString().length() == 0)
{
pickuptimejava.setError("Enter pickuptime");
validated = false;
}
if(numberstuffjava.getText().toString().length() == 0) {
numberstuffjava.setError("Enter number of clothes");
validated = false;
}
// fire in the hole !
if (validated)
{
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
}
The else condition it's only attached to the numberstuffjava if condition, in this scenario the else condition will be called always when this if condition is false.
Try changing the previous if conditions with else if.
You just create a method which return true only if all your needs are satisfied. if the method return true, then only go to next activity. I will give an example here.
public boolean isValid(){
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
return false;
}
if(firstnamejava.getText().toString().length() == 0)
{
firstnamejava.setError("Whats your first name?");
return false;
}
return true;
}
Then do things like below.
if(isValid()){
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
just add else if for checking all validation
if (v == nextbuttonjava) {
if (edittext1.getText().toString().length() < 5) {
edittext1.setError("error");
} else if (edittext2.getText().toString().length() == 0) {
edittext2.setError("error");
} else {
// your intent here
}
}
add else if for every of your edittext .
Try out below method to check an empty value of edittext.
public static boolean m_isError = false;
/**
* This is method to check edit text empty or not.
*
* #param p_editText
* - edittext
* #param p_nullMsg
* -display message if edittext null
*/
public static void validateForEmptyValue(EditText p_editText,
String p_nullMsg) {
if (p_editText != null && p_nullMsg != null) {
// use trim() while checking for blank values
if (TextUtils.isEmpty(p_editText.getText().toString().trim())) {
m_isError = true;
p_editText.setError(p_nullMsg);
}
}
}
Use the above method as below for each of your edittext:
CustomValidator.m_isError = false;
CustomValidator.validateForEmptyValue(metfirstname, "enter firstname", metfirstname);
CustomValidator.validateForEmptyValue(metlastname, "enter lastname", metlastname);
if (!CustomValidator.m_isError) {
//Define your logic once all the validations are done.
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
I have a submit button in my program/JFrame it checks for validations and brings up error messages and then another form pops up and has all entered details:
private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
//VALIDATIONS-----------------------------------------------------------
if(txtName.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
}
else
{
jlblNameVer.setVisible(false);
}
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
}
//EMAIL VALIDATION
if(txtEmail==null ||txtEmail.getText().length() < 10|| txtEmail.getText()== null ||!(txtEmail.getText().trim().contains("#") && txtEmail.getText().trim().contains(".")))
{
JOptionPane.showMessageDialog(null, "Invalid Email");
}
//Phone Number Validation
if(txtPhoneNum.getText().length() < 10)
{
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
}
//COMBOBOX VALIDATIONS
if(cmbStayDuration.getSelectedIndex() == -1)
{
JOptionPane.showMessageDialog(null, "Please select stay duration");
}
//Photo ID
if(cmbPhotoId.getSelectedIndex() == -1)
{
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
}
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
The problem is even when these validations are wrong the form will pop up anyway
If any of the validations are incorrect I don't want the popup form to show, what must I do?
You either need to restructure you if blocks into a if-else if-else block, so that last (else) condition would show the new view or use a boolean field which determines if the validation passed or not, for example...
boolean passed = true;
if (txtName.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
passed = false;
} else {
jlblNameVer.setVisible(false);
}
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
passed = false;
}
//EMAIL VALIDATION
if (txtEmail == null || txtEmail.getText().length() < 10 || txtEmail.getText() == null || !(txtEmail.getText().trim().contains("#") && txtEmail.getText().trim().contains("."))) {
JOptionPane.showMessageDialog(null, "Invalid Email");
passed = false;
}
//Phone Number Validation
if (txtPhoneNum.getText().length() < 10) {
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
passed = false;
}
//COMBOBOX VALIDATIONS
if (cmbStayDuration.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select stay duration");
passed = false;
}
//Photo ID
if (cmbPhotoId.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
passed = false;
}
if (passed) {
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
or
if (txtName.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
} else {
jlblNameVer.setVisible(false);
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
passed = false;
} else if (txtEmail == null || txtEmail.getText().length() < 10 || txtEmail.getText() == null || !(txtEmail.getText().trim().contains("#") && txtEmail.getText().trim().contains("."))) {
JOptionPane.showMessageDialog(null, "Invalid Email");
passed = false;
} else if (txtPhoneNum.getText().length() < 10) {
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
passed = false;
} else if (cmbStayDuration.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select stay duration");
passed = false;
} else if (cmbPhotoId.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
passed = false;
} else {
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
}
You should also have a look at The Use of Multiple JFrames, Good/Bad Practice? and maybe consider using a CardLayout instead, but this will depend on your underlying needs
You could also have a look at Validating Input, which would allow you to do post validation of your fields when the lose focus
Because the eclipse was error:
else delete token????
the code is this:
public void go(View v)
{
String a= url.getText().toString();
if (a.contains("www.") == true); {
webView.loadUrl(url.getText().toString());
}
else {
String search = "https://www.google.com/search?q="+url.getText();
webView.loadUrl(search);
}
}
PLease help me
An if statement requires a body - remove the semi-colon
if (a.contains("www.") == true); {
^
if (a.contains("www.") == true);
==
if (a.contains("www.") == true) { }
Which does nothing. So webView.loadUrl(url.getText().toString()); will be always executed since it's in an inner block.
If you're using an IDE, you can make it warn you of things like that (empty if statements).
You're getting an error because your code is:
if (a.contains("www.") == true) { }
{
//some code
}
else {
}
But the else has no if before it..
Replace this
if (a.contains("www.") == true); {
with this
if (a.contains("www.") == true) {
I can't find the problem that causes my while loop not to work.
When I run the program and press a radio button, I get this error code:
Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Here is my loop:
int i = 1;
boolean x;
//for (i = 0; i < 6; i++) {
do{
warning.setText(" FEL!");
i++;
while(x == false);{
if(e.getSource() == buttonOK){
if(buttonDollar.isSelected() == false){
x = false;
}
if(buttonEuro.isSelected() == false){
x = false;
}
if(buttonPund.isSelected() == false){
x = false;
}
if(buttonKrona.isSelected() == false){
x = false;
}
break;
}
}
}
You are missing the syntax for "while" element
From the sun site (I am guessing that this is java)
do {
statement(s)
} while (expression);
I think you need a closing curly brace before the while
do{
warning.setText(" FEL!");
i++;
}while(x == false);
The whole structure is all wrong, you're looking to do something very simple:
if(e.getSource() == buttonOK)
{
if( !buttonDollar.isSelected() && !buttonEuro.isSelected()
&& !buttonPund.isSelected() && !buttonKrona.isSelected() )
{
warning.setText(" FEL!");
}
}
From a UI perspective, it's better to ensure that one radio button is always selected (as this is what your users will likely expect).