Using regular expressions in iOS to determine the input is 8-16-bit and contains both numbers and letters

Source: Internet
Author: User

Today in the project need to judge the user entered the length of the user name 8-16-bit and contains both numbers and letters, the online search for the use of regular expressions, and then refer to this blog, the perfect solution to the problem. Record it:

The password has the following requirements: It consists of numbers and letters, and contains both numbers and letters, and the length is between 8-16 bits.

How do I analyze requirements? Split it! This is the general idea of software design. The split requirements are as follows:
1, not all numbers.
2, not all letters.
3, must be a number or a letter
As long as the above 3 requirements can be satisfied, write to the following:

1
^(?![0-9]+$)(?![a-za-z]+$)[0-9a-za-z]{8, -}$

Separate to annotate:
^ matches the beginning of a line
(?! [0-9]+$] predict that the position is not all numbers behind
(?! [a-za-z]+$] predict that the position is not all letters behind
[0-9a-za-z] {8,16} consists of 8-16 digits or this letter
$ Match Line End position

Note: (?! XXXX) is the negative 0-wide assertion of a regular expression of a form that identifies the pre-position after it is not the XXXX character.


Code that is used in iOS:

* *  @author KaKa, 15-07-31 14:07:02 * * To  Determine whether the password entered by the user is compliant with the code requirements:    1. Length greater than 8 bits    2. The password must contain both the number and the letter */+ ( BOOL) Judgepasswordlegal: (NSString *) pass{    BOOL result = false;    if ([Pass length] >= 8) {        //judgment length is greater than 8 digits and then determine if both numbers and characters are included        NSString * regex = @ "^ (?! [0-9]+$) (?! [a-za-z]+$] [0-9a-za-z]{8,16}$];        Nspredicate *pred = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];        result = [pred evaluatewithobject:pass];    }    return result;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using regular expressions in iOS to determine the input is 8-16-bit and contains both numbers and letters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.