2

I want to use regex in my android application to validate some field.

User Name :

1 Capital Letter[A-Z], 2 digit[0-9], 1 Special Character any and then followed by small character[a-z] and lenth would be 10 character max.

Email Address :

Must contain @google.com in last

Mobile :

Must be +91 and after that 10 digit.

How can I form my regex pattern for all three fields..?

Community
  • 1
  • 1
user1642834
  • 33
  • 1
  • 1
  • 3

6 Answers6

4

Regx for emailid:

^[A-Za-z][A-Za-z0-9]*([._-]?[A-Za-z0-9]+)@[A-Za-z].[A-Za-z]{0,3}?.[A-Za-z]{0,2}$

accepts values as:

  • hdf4.j8k@bfv.djf
  • ds.sd@c25v.fdv
  • dv_sdv@fvv
  • vdf-f@jn.fdv
  • jfk@mbf.khb.in

n etc

Regex for Mobile No:

^[7-9][0-9]{9}$

works perfect for indian mobile numbers.

Regex for landline No:

^[0-9]{3,5}-[2-9]{1}[0-9]{5,7}$

for landline numbers in india with region code

eg: 022-58974658

Siddhesh
  • 1,370
  • 11
  • 28
1

You can find the regex you require for password, email and more , for instance

For Username :

^[a-z0-9_-]{3,15}$

^ # Start of the line

[a-z0-9_-] # Match characters and symbols in the list, a-z, 0-9 , underscore , hyphen

{3,15} # Length at least 3 characters and maximum length of 15

$ # End of the line

at : http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/

Sunny Kumar Aditya
  • 2,806
  • 4
  • 26
  • 38
0

Please see below link for Email Validation and you can modify some part of the code for username validation and phone number validation.

how-to-check-edittexts-text-is-email-address-or-not

Community
  • 1
  • 1
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0
public static boolean isEmailValid(String email) {
    boolean isValid = false;

    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence inputStr = email;

    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}
shassss
  • 321
  • 1
  • 13
  • No need for a flag, just return true inside the if and false outside. –  Sep 03 '12 at 05:39
0

Mobile :

Must be +91 and after that 10 digit.

^[7-9][0-9]{9}$ is useful for only mobile numbers. But for country code we should have to use regex like ^[+(00)][0-9]{6,14}$ ..

something like

1)

String phoneNumber = "+919900990000"
if(phoneNumber.matches("^[+(00)][0-9]{6,14}$")){
   //True

2)

  String phoneNumber = "9900990000"
  if(phoneNumber.matches("^[+(00)][0-9]{6,14}$")){
   //False

The first is true because it has the country code with it, But the second one is false because it has not any country code attached with it.

Ranjit
  • 5,130
  • 3
  • 30
  • 66
0

You can use Patterns class for validating factors such as email,mobile no etc. Here's how:

public static boolean isValidEmail(CharSequence target) {
    return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
}

public static boolean isValidMobile(CharSequence target) {
    return (!TextUtils.isEmpty(target) && Patterns.PHONE.matcher(target).matches());
}

Hope it'll help you.

Nevil Ghelani
  • 687
  • 10
  • 10