Android verifies that mailbox input is legitimate

Time:2019-10-7

In this paper, we share the specific code of Android to verify the validity of mailbox input. For your reference, the details are as follows.

Here’s how to verify the validity of mobile phone number and mailbox. You can use these two static methods directly in your own project.

public class MainActivity extends Activity implements View.OnClickListener {

 private Button btn;
 private EditText et;
 private TextView tv;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  btn = (Button) findViewById(R.id.btn);
  et = (EditText) findViewById(R.id.et);
  tv = (TextView) findViewById(R.id.tv);
  btn.setOnClickListener(this);
 }

 @Override
 public void onClick(View view) {
  switch (view.getId()) {
   case R.id.btn:

    String number = et.getText().toString();
    boolean judge = isMobile(number);
    if (judge == true) {
     Tv.setText (mobile phone number is legal).
    } else {
     Tv.setText ("mobile phone number is illegal");
    }
    break;
  }
 }


// /**
// * verify cell phone format.
//  */
 public static boolean isMobile(String number) {
 /*
 Mobile: 134, 135, 136, 137, 138, 139, 150, 151, 157 (TD), 158, 159, 187, 188
 Unicom: 130, 131, 132, 152, 155, 156, 185, 186
 Telecom: 133, 153, 180, 189, (1349 Weitong)
 To sum up, the first must be 1, the second must be 3 or 5 or 8, and the other places can be 0-9.
 */
 // "[1]" means the first digit is 1, "[358]" means the second digit can be one of 3, 5 and 8, and " D {9}" means the following digit can be 0-9, with nine digits.
  String num = "[1][358]\d{9}";
  if (TextUtils.isEmpty(number)) {
   return false;
  } else {
   // matches (): Whether a string matches in a given regular expression
   return number.matches(num);
  }
 }
// Mailbox Verification
 public static boolean isEmail(String strEmail) {
  String strPattern = "^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
  if (TextUtils.isEmpty(strPattern)) {
   return false;
  } else {
   return strEmail.matches(strPattern);
  }
 }


}

Understanding regular expressions

learning process

1. Concept: “Regular Expression” is a special string used to match and find strings.

2. In the Java infrastructure package:
The java.util.regex package consists of three main classes: Pattern, Matcher and PatternSyntaxException.

3. The role of regular expressions:

1). Judging the correctness of strings
2). Find strings
3. Use in other classes, especially String

// 1. Judging the correctness of strings
 // Determine whether the phone number is an 11-digit number starting at 136
 private void m1(String s) {
  // Mode 1:
  // boolean b = Pattern.matches("136\d{8}", "13698987654");
  // Mode 2:
  Pattern p = Pattern.compile("136\d{8}");
  Matcher M = P. matcher (s); // Regular Expression Telephone Number
  Boolean B = M. matches (); // Determine whether a match is made
  System.out.println(b);// true
}
// 2. Find out if there is a substring of two numbers in the string
 private void m3() {
  Pattern p = Pattern.compile("\d{2}");
  Matcher m = p.matcher("a29abbccsdf");
  boolean b = m.find();
  System.out.println(b);// true
 }
// 3. Use in other classes, especially String
 // String disassembly by operator
 public void m4() {
  // String s = "23+234-123*234/1000";
  // String[] a = s.split("\+|\-|\*|/");

  String s = "11+22+33*66";
  String[] a = s.split("\*");
  for (String string : a) {
   System.out.println(string);
  }
 }
// 4. Example: Determine whether a string ends with an ax beginning by and only one character in the middle
public class Test {
 public boolean m1(String str) {

// Mode 1:
// Pattern p = Pattern.compile("ax.by");
// Matcher m = p.matcher(str);
// boolean b = m.matches();

// mode two
 boolean b = Pattern.matches("ax.by", str);
 return b;
 }
 public static void main(String[] args) {
 Test t = new Test();
 System.out.println(t.m1("axmby"));
 }
}

Tip 1: Regular expressions can also find matching substrings in strings; they can be used in other classes. … (Later chapters will talk about)

5. hint 2:

Patterns object represents a compiled regular expression. The Pattern class does not provide a common constructor. A common static compile method must be invoked, which returns a Pattern object.
Matcher is an object that parses the pattern and completes the matching operation depending on the input string. Matcher also does not define a common constructor, and requires a Matcher object to be obtained by calling the matcher method of the Pattern object.
PatternSyntaxException object is an unchecked exception indicating a grammatical error in a regular expression.

Regular expression wildcards

learning process

I. Universal Pitch Character Matching

1.”.” —> Used to match an arbitrary character

For example, “t.n”, which matches “tan”, “ten”, “tin” and “ton”, also matches “t# n”, “tpn” and even “t n”.
Pattern p = Pattern.compile(“a.b.c.d”);
Matcher M = P. matcher (“axbac2d”); // regular expression phone number
Boolean B = M. matches (); // Determine whether a match is true

2.”[]” —>To solve the problem of too wide a range of “.” matches, you can specify meaningful characters in square brackets (“[]”). At this point, only the characters specified in square brackets participate in the matching. That is to say, the regular expression “t [aeio] n” only matches “tan”, “Ten”, “tin” and “ton”. But “Toon” does not match because within square brackets you can only match a single character:

3.”|”—>If you want to match “toon” in addition to all the words (tan,””Ten,””tin”and”ton”matched above, you can use the”|”operator. The basic meaning of the | operator is the OR operation. To match “t OO n”, use the “t (a | e | I | o | oo) n” regular expression. Extensions cannot be used here, because square brackets only allow matching of individual characters; parentheses “()” must be used here to represent a whole in the same way as in arithmetic expressions.

4. “-” denotes the range of numbers or letters.

[0-9] denotes one of the characters from 0 to 9, and [3-8] denotes one of the characters from 3 to 8.
[a-z] denotes one of the characters from a to z, and [b-m] denotes one of the characters from B to M.
[A-Z] represents one of the characters from A to Z
[b-d3-8] denotes one of the characters B to Z or 3 to 8
[a-d x-z] denotes one of the characters from a to D or X to Z

Example:

[a-c] [0-9] {3} can match a234, b112, c223, etc.

5.”^” —> Namely, the symbol used to determine that the symbol immediately to the right of the symbol cannot appear

5.1 [^ 0-9] means that one character cannot be considered 0 to 9
5.2 [^ A] means a character that cannot be considered A
5.3 [^ a-c] means characters that cannot be a to C

6. Shortcut Symbols

7. “” denotes an escape

If the character to be used in a regular expression conflicts with the wildcard character, the word “” is preceded by the word “” to indicate that it is not used by the wildcard character.
For example, “[0-9] {3}* {2} [0-9] {2} a*” is not a number of times, but a character. It can match three numbers starting from 0 to 9, followed by two *, followed by two numbers from 0 to 9, and finally zero to more than a characters.
This “234** 23a” matches the regular expression above.

8. “”Expresses the EndEvery regular expression can be followed by “to indicate the end, every regular expression can be followed by adding, essentially adding and not adding the same effect

9. Special Matching

[\u4e00-\u9fa5] matching Chinese characters
The example “[\u4e00-\u9fa5]{3}” can match three Chinese character strings.

2. Universal Pitch Character of Matching Number

These symbols are used to determine the number of occurrences of symbols close to the left of the symbol:

Example 1: a * Hello can match strings ending with Hello and beginning with 0 or more a, such as Hello, aHello, aaHello, aaaHello
Example 2: MyA* Hello can be matched to begin with My and end with Hello, with 0 or more A in the middle, such as MyAAAHello
Example 3: Hello E + World can be matched to begin with Hello and end with World, with one or more E in the middle, such as Hello EE World
Example 4: Hello E? World can be matched to begin with Hello and end with World. There must be 0 to 1 E in the middle.
Example 5: HelloE {3} World can be matched to start with Hello and end with World. There must be three E’s in the middle.
Example 6: HelloE {2,4} World can be matched to begin with Hello and end with World, with 2 to 4 E in the middle.
Example 7: Hello [^ a B c] can match starting with Hello, not ending with a or B or C

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.