PHP is a regular judge can only enter alphabetic characters and digital examples

Source: Internet
Author: User
Tags strlen

Digital Regular Filter

The code is as follows Copy Code

if (Preg_match ("/^\d*$/", $yebihai))
Echo (' is a number ');
Else
Echo (' not number ');


or use a function

if (Is_numeric ($yebihai))
Echo (' is a number ');
Else
Echo (' not number ');

The difference between the two methods is that the Is_numeric decimal is also considered a number, and the decimal point is preceded by a character.

Regular expressions are the following numbers and letters:

The code is as follows Copy Code

$password = ' ABC12 ';
if (!preg_match (/^) ([a-z]+[0-9]+) | ( [0-9]+[a-z]+)] [a-z0-9]*$/i, $password)) {
Echo ' Password must be a combination of numbers and letters ';
}

If you want to limit the length of 6-15-bit English characters and numbers

As long as you add a strlen to judge the following $plen<6| | $plen >15

The code is as follows Copy Code

$password = ' abc123 ';
$plen =strlen ($password);
if (!preg_match (/^) ([a-z]+[0-9]+) | ( [0-9]+[a-z]+)] [a-z0-9]*$/i, $password) | | $plen <6| | $plen >15) {
Echo ' Password must be a combination of 6-15 digits and letters ';
}

In fact, we can also use regular to limit the length

We can refer to the rules for length.

Verify the N-bit number: ^\d{n}$
Verify at least n digits: ^\d{n,}$
Verify the number of m-n bits: ^\d{m,n}$

The above is a number, we can combine the verification of just add a {} on it.

The code is as follows Copy Code

$password = ' abc123 ';
$plen =strlen ($password);
if (!preg_match (/^) ([a-z]+[0-9]+) | ( [0-9]+[a-z]+)] [a-z0-9]*{8,}$/i, $password) | | $plen <6| | $plen >15) {
Echo ' Password must be a combination of 8 digits and letters ';
}

The length is 8 or more in fact, just add a {8 after the regular,} on it,

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.