2011-03-21 45 views
0

在PHP中,我如何验证用户输入,如下面的示例所示。如何验证第一个字符必须以A-Z开头

例有效输入

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

例无效输入

$input ='2sdf23'; 
$input ='2121adsasadf'; 
$input ='23142134'; 

回答

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

或使用[:alpha:]如果你不想使用[a-z](例如,如果你需要认识重音字符)。

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

您可以尝试使用正则表达式,与preg_match()功能:

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

基本上,你搜索:

  • 开头的字符串:^
  • 一个字母: [a-zA-Z]
6
if(ctype_alpha($input[0])){ 
//first character is alphabet 
} 
else { 
//first character is invalid 
} 
相关问题