开发者

How do I validate a first character must begin with A-Z

In PHP how do I validate user input like an example below.

Example valid input

$input ='abkc32453';
$input ='a32453';
$input ='dsjgjg';

Example invalid input

$input ='2sdf23';
$input ='2121adsasadf';
$input =开发者_开发技巧'23142134';


if(ctype_alpha($input[0])){
//first character is alphabet
}
else {
//first character is invalid
}


if (preg_match('/^[a-z]/i', $input)) { /*   "/i" means case independent */
    ...
}

or use [:alpha:] if you'd rather not use [a-z] (e.g. if you need to recognise accented characters).


You might try using a regular expression, with the preg_match() function :

if (preg_match('/^[a-zA-Z]/', $input)) {
    // input is OK : starts with a letter
}

Basically, you search for :

  • beginning of string : ^
  • one letter : [a-zA-Z]


preg_match('%^[a-zA-Z].*%', $input, $matches);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜