password minimum and maximum character - android

I'm trying to have the maximum character of password that i want to set in my android apps. What would be the best way to code it ? I only have the minimum of 6 character and i want to have maximum of 10 character set password. Here is my attempt:
public void UserSignUp() {
if(ET_PASSWORD_SIGNUP.getText().length() >0 && ET_CONFIRM_PASSWORD_SIGNUP.getText().length() >0 &&
ET_PASSWORD_SIGNUP.getText().length() >5 && ET_CONFIRM_PASSWORD_SIGNUP.getText().length() >5)
{
if(ET_PASSWORD_SIGNUP.getText().toString().equals(ET_CONFIRM_PASSWORD_SIGNUP.getText().toString())) {
String SignUpResult = "";
db = DBMANAGER.getWritableDatabase();
SignUpResult = CONTENTMANAGER.saveUserPassword(ET_PASSWORD_SIGNUP.getText().toString(),this);
Toast.makeText(getApplicationContext(), SignUpResult, Toast.LENGTH_SHORT).show();
ActivityLogIn();
} else {
Toast.makeText(getApplicationContext(), "Confirm Password and Password did not match!",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Invalid Input", Toast.LENGTH_SHORT).show();
}
}

Not an answer as such but another solution,
Have a look at https://github.com/ragunathjawahar/android-saripaar, nice annotation library.
You can annotate your password fields and it will handle the logic for you...
#Password(order = 1, minLength = 4, maxLength = 10)
private EditText passwordEditText;
#ConfirmPassword(order = 2)
private EditText confirmPasswordEditText;

private boolean checkPW(String pw, String pw2) {
if(!pw.equals(pw2)){
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pws_do_not_match), Toast.LENGTH_LONG).show();
return false;
}
if (pw.length() < 4) {
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pw_too_short), Toast.LENGTH_LONG).show();
return false;
}
if(pw.length() > 10){
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pw_too_long), Toast.LENGTH_LONG).show();
return false;
}
//continue here
return true;
}
});
}

Check this code..
if (ET_PASSWORD_SIGNUP.getText().toString().length() < 6
|| ET_PASSWORD_SIGNUP.getText().toString().length() > 10) {
ET_PASSWORD_SIGNUP.requestFocus();
ET_PASSWORD_SIGNUP.setError("Password should be between 6 to 10 characters long");
}

you can write this code:
// check password
public boolean checkPassword(String password, String confirmPassword) {
boolean state = false;
int passLength = password.length();
if (passLength >= 6) {
if (confirmPassword.length() != 0) {
if (password.equals(confirmPassword)) {
state = true;
} else {
makeToast("Password does't match!");
inputConfirmPassword.setText("");
inputPassword.setText("");
inputPassword.requestFocus();
}
} else {
Toast.makeText(this, "Please enter confirm password",
Toast.LENGTH_SHORT).show();
inputConfirmPassword.setText("");
inputConfirmPassword.requestFocus();
state = false;
}
} else {
Toast.makeText(this, "Please enter 6 digit password",
Toast.LENGTH_SHORT).show();
inputPassword.setText("");
inputPassword.requestFocus();
state = false;
}
return state;
}

Related

didn,t work checking between password and repeat password in android

........................................
else if (e8.getText().toString().length() == 0) {
e8.setError("Password is required");
Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_SHORT).show();
i = 0;
}
else if (e9.getText().toString().length() == 0) {
e9.setError("Password is required");
Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_SHORT).show();
j = 0;
}
else if (!e8.equals(e9.getText()))
{
e9.setError("Both Passwordsxmvbxb are different");
Toast.makeText(getApplicationContext(), "Please enter Correct Password", Toast.LENGTH_SHORT).show();
k = 0;
}
........................................
Try This
}
else if (e9.getText().toString().equals("")) {
e9.setError("Password is required");
Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_SHORT).show();
j = 0;
}
else if (!e8.getText().toString().equals(e9.getText().toString()))
{
e9.setError("Both Passwordsxmvbxb are different");
Toast.makeText(getApplicationContext(), "Please enter Correct Password", Toast.LENGTH_SHORT).show();
k = 0;
}
try this approach.
if(validate()){
// perform operation
}
method declaration:
private boolean validate()
{
if (TextUtils.isEmpty(edtPassword.getText())) {
//toast enter password
return false;
} else if (TextUtils.isEmpty(edtConfirmPassword.getText())) {
//toast enter confirm password
return false;
} else if (!edtPassword.getText().toString().equals(edtConfirmPassword.getText().toString())) {
//toast password not match
return false;
}
return true; // considering all conditions are true
}
It should be String when you are checking any two string. currenty you are doing e8.equal where e8 is your EditText so it will check for equal object. It should be
if(!e8.getText().toString().equals(e9.getText().toString()))
use .equals instead of == to compare string .

How to ensure only even numbers are entered in edittext in textwatcher?

I need to enter only even numbers(range:0-20) in edittext in textwatcher. However, when i did the following, it remained same that is it is allowing both even and odd numbers to be entered. I want when a user enters an odd number the toast is displayed. guide me please!
even
#Override
public void afterTextChanged(Editable s) {
try {
int v = Integer.parseInt(s.toString());
if(v%2!=0){
if (v > 20) {
s.replace(0, s.length(), "", 0, 2);
} else if (v < 0) {
s.replace(0, s.length(), "", 0, 1);
}
}
} catch (NumberFormatException ex)
{
Toast.makeText(MainActivity.this, "invalid", Toast.LENGTH_LONG).show();
}
}
You are just showing the Toast when a NumberFormatException exception is thrown, meaning that the string does not contain a number.
So you have to show the toast also when the number is even, and you will do it on the else condition of
if(v%2 != 0){
//Number is odd
}else{
//Number is even
}
Try like the following:
#Override
public void afterTextChanged(Editable s)
try {
int v = Integer.parseInt(s.toString());
if(v>0 && v<20){
if(v%2 != 0){
Toast.makeText(getContext(), "ODD", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getContext(), "EVEN", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(getContext(), "OUT OF RANGE", Toast.LENGTH_SHORT).show();
}
}catch (NumberFormatException e){
Toast.makeText(getContext(), "INVALID", Toast.LENGTH_SHORT).show();
}
}

android enable and disable button in registration screen

I created registration screen i want to disable submit button and it should enable only when all the fields get correct input, bellow i added list of text fields these fields must get correct input and submit button must enable.
protected boolean validateData() {
if (email.getText().toString().trim().length() == 0) {
email.setError("Email-ID/Registered Number can not be empty!");
return false;
} else if (!(email.getText().toString().trim().contains("#"))
&& email.getText().toString().trim().length() < 10) {
email.setError("Email/Registered Number not valid!");
return false;
} else if (!(email.getText().toString().trim().contains("."))
&& email.getText().toString().trim().length() < 10) {
email.setError("Email/Registered Number not valid!");
return false;
}
else if (!(email.getText().toString().trim().contains("#"))
&& !(email.getText().toString().trim().contains("."))
&& email.getText().toString().trim().length() > 10) {
email.setError("Email/Registered Number not valid!");
return false;
}
else if (email.getText().toString().trim().contains("#")
|| email.getText().toString().trim().contains(".")) {
if (!WebServiceSingleTon.getInstance().isEmailValid(
email.getText().toString().trim())) {
email.setError("Email is not valid!");
return false;
} else if (password.getText().toString().trim().length() == 0) {
password.setError("password can not be empty!");
return false;
} else if (password.getText().toString().trim().length() < 6) {
password.setError("Your password need to have minimum 6 characters!");
return false;
} else if (password.getText().toString().trim().length() > 20) {
password.setError("Your password need to have maximum 20 characters!");
return false;
}
} else if (password.getText().toString().trim().length() == 0) {
password.setError("password can not be empty!");
return false;
} else if (password.getText().toString().trim().length() < 6) {
password.setError("Your password need to have minimum 6 characters!");
return false;
} else if (password.getText().toString().trim().length() > 20) {
password.setError("Your password need to have maximum 20 characters!");
return false;
}
return true;
}
to make a button "disabled" You'll need to set it's android:clickable property to false. Then, set a custom background on it that changes its background to gray (or however you want to it look while its disabled). Then after you call that validateData() function you can check to see if you should change the background of the button to be "enabled"

How to check validate in android? and if both intime and outtime are empty then Toast or both of one empty then passed

if both intime and outtime are empty then Toast or both of one empty then passed
private boolean isValidate() {
// TODO Auto-generated method stub
if(sp.getSelectedItem().toString().trim().length() == 0){
Toast.makeText(getApplicationContext(), "Please Enter Name !", Toast.LENGTH_LONG).show();
return false;
}else{
}
if(etDate.getText().toString().trim().length() == 0){
Toast.makeText(getApplicationContext(), "Please Enter Date !", Toast.LENGTH_LONG).show();
return false;
}else{
}
//both intime and out time passed either one of passed here
if(etintime.getText().toString().trim().length() == 0){
Toast.makeText(getApplicationContext(), "Please Enter In Time !", Toast.LENGTH_LONG).show();
return false;
}else{
}
if(etouttime.getText().toString().trim().length() == 0){
Toast.makeText(getApplicationContext(), "Please Enter Out Time !", Toast.LENGTH_LONG).show();
return false;
}else{
}
return true;
}
Use && conditional operator to perform logical And, in your case check both of your EditTexts if they are empty.
e.g.
if(editText1.getText().toString().equals("") && editText2.getText().toString().equals("")){
//show Toast here.
}

Assigning a Variable in EditText

I am a newbie in Android. Can you teach me on how to assign a variable in editText? See code the example below.
EditText assignVariable = (EditText) findViewById(R.id.variable);
if(assignVariable == 12 ) {
Toast.makeText(getApplicationContext(), "Assign Varialbe Successfull", Toast.LENGTH_SHORT);
return true;
} else {
Toast.makeText(getApplicationContext(), "Not Successfully Assign!", Toast.LENGTH_SHORT);
return true;
}
If you want to set text in edittext, then you can use:
assignVariable.setText("your text goes here");
If you need to retrieve text from edittext, you would need to put this code on button click. (not just after initialization of EditText.)
String assignedText = assignVariable.getText().toString();
And you can't compare String with == operator. You should use equals or equalsIgnoreCase
if (assignedText.equals("12")) {
Toast.makeText(getApplicationContext(), "Assign Varialbe Successfull", Toast.LENGTH_SHORT);
return true;
} else {
Toast.makeText(getApplicationContext(), "Not Successfully Assign!", Toast.LENGTH_SHORT);
return true;
}
Hope this helps.
EditText assignVariable = (EditText) findViewById(R.id.variable);
if(assignVariable.getText().toString().equals("12") ) {
Toast.makeText(getApplicationContext(), "Assign Varialbe Successfull", Toast.LENGTH_SHORT);
return true;
} else {
Toast.makeText(getApplicationContext(), "Not Successfully Assign!", Toast.LENGTH_SHORT);
return true;
}

Categories

Resources