开发者

how to check if the inputted data is string in php [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Alphabetic equivalent of PHP is_numeric

If you check开发者_开发百科 if the data is a number, we use is_numeric:

if(!isset($_POST['rnum'])||trim($_POST['rnum'])==""){echo "Error:Enter Room Number!";     
  echo '<input type="button" value="Back" onClick="history.go(-1);return true;">';
  }else if(!is_numeric(trim($_POST['rnum']))){echo "Error: Enter A Numeric Value!";
  echo '<input type="button" value="Back" onClick="history.go(-1);return true;">';
die();
  }

How about checking if it is string?or expecting that the inputted data are just letters


You can try using regex like:

if(preg_match('/^[a-z]+$/i',$input_to_be_tested))
 echo 'Has only letters';
else
 echo 'Has non-letters';

If you wan to allow spaces in the input you can try:

/[\sa-z]+/i


DO you only want alphabetic characters? You could use a regular expression to match against [a-zA-Z]+, but that would only get letters of the alphabet. Are other characters allowed or not?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜