0

So I have this row of regex to check my input. There is an input because the else is activated but I can't find it.

Username: Donald
Password: 1234
Name: Donald
LastName: Muylle
Phone: 012121212
Address: Zeverstreet 12
City: Gestel
Job: Bar

if(txtUsername.getText().matches("[a-zA-Z0-9]")
&& txtPassword.getText().matches("[a-zA-Z0-9]") &&
txtName.getText().matches("[a-zA-Z]") && 
txtLastName.getText().matches("[a-zA-Z]") && 
txtPhone.getText().matches("[0-9]") &&
txtAddress.getText().matches("[a-zA-Z0-9\\s]") && 
txtCity.getText().matches("[a-zA-Z]") && 
txtJob.getText().contains("[a-zA-Z]")){
    // Code
} else {
    // Shows Alert
}

3 Answers 3

1

All you have to do is to add + quantifier next to all the character classes like,

txtUsername.getText().matches("[a-zA-Z0-9]+")
                                          ^

So that, it would match one or more occurrences of the characters given in the list. Without +, it would match a single character only where the matches method must check for a match against the whole string. So this would end up in failure if the input contain more than one character.

Sign up to request clarification or add additional context in comments.

5 Comments

For some weird reason, still showing my error. It didn't fix the problem. Thanks though.
It's an error button I made my self that just says 'Credentials not filled in correctly.'
it seems like error caused by some part of your program not the regex.
I already debugged all the rest and ended up in the regex because if I system.out.println('bla') in the ELSE part, it would show up, so that makes my attention go to the regex being a failure.
I think I found it, the last matches is 'contains' and not 'matches'. How did I get that :D Still thanks for the help!
1

You have contains in the last check. Replace it with matches.

Comments

1

All your regular expressions match only strings of 1 char. Modify all them adding a + symbol at the end.

So [a-zA-Z0-9] becomes [a-zA-Z0-9]+, [a-zA-Z] becomes [a-zA-Z]+ and so on.

The address regular expression [a-zA-Z0-9\\s] should be [a-zA-Z0-9\s]+ with only one slash

3 Comments

For some weird reason, still showing my error. It didn't fix the problem. Thanks though.
There is also another problem in the address regular expression.
There is also another problem in the address regular expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.