简体   繁体   中英

How would I use regex to check for a value that has two fixed uppercase letters followed by numeric values in PHP or jQuery?

An example would be "SU1203" or "UP1234" or any two letters followed by numeric values. thanks

This can be solved with the quite simple expression

^[A-Z]{2}\d+$

In JavaScript you can use the test() method:

if(/^[A-Z]{2}\d+$/.test(str))

and in PHP, preg_match :

if(preg_match('/^[A-Z]{2}\d+$/', $str) === 1)

I suggest to learn regular expressions .

See also:

Try:

if (preg_match('|^[A-Z]{2}[0-9]+$|', $string_to_test))
{
  // matched
}

javascript example:

if ( /(^[A-Z]{2}\d+)/.test('SU1203') ) {
   alert( 'matched' )
} else { alert('not matched') }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM