-1

Let us suppose that you want the following in the edit text fields. What regex would you use?

validation

For the email I used this:

if(!email.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")) {
    etEmail.setError("Invalid Email Address");
}

But how do I go about with the others?

Thanks in advance!

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
Theo
  • 3,099
  • 12
  • 53
  • 94
  • What would you call a special character? – Robo Mop Apr 23 '18 at 18:00
  • How many QAs do you find if you search StackOveflow for "password regex" and for "phone number regex" ? Please explain for each of them, why it does not help you, I cannot find anything in your question which makes it different enough from the others. So I do not really understand the specific problem you have. I.e. your question is unclear to me. – Yunnosch Apr 23 '18 at 18:03
  • 1
    @Theo I've finished my answer, feel free to refer to it, and point out any specification of yours that it may not have met :) – Robo Mop Apr 23 '18 at 18:16
  • [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/48346033#48346033) may really benefit you – ctwheels May 04 '18 at 14:51

2 Answers2

2

For the password, you could try:

if(!(password.length() >= 6) || !password.matches(".*?[A-Z].*") || !password.matches(".*?[a-z].*") || !password.matches(".*?[0-9].*") || !password.matches(".*?[\\W\\-_].*")) {
    System.out.println("Password Invalid");
}

Explanation -

.*? - indicates that the regex may start with zero-or-more characters, before being followed by<br>
    [A-Z]     - a Capital Letter from 'A' to 'Z'
    [a-z]     - a lowercase letter from 'a' to 'z'
    [0-9]     - a digit from 0-9
    [\\W\\-_] - a non-word character (not letter, digit or dash), a dash, or underscore
.* - indicates that after the special character (or whatever the requirement for that condition is), there may be zero-or-more characters.

This approach may seem a bit lengthy, but is certainly easier to read and understand.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
0

for name :- if(!name.matches("[a-zA-Z0-9]")){ //show error here}

for email : if(!Patterns.EMAIL_ADDRESS.matcher(target).matches()){ /show error here}

for mobile :- if(mobile.length<10){ //show error here }

Madhav
  • 317
  • 2
  • 12