很抱歉用一个对你们中的一些人来说似乎相当直率的问题来打扰你们,但我想知道你们中是否有人可以在验证方面提供一些启发
我有一个文本域,我需要对一个手机号码进行验证,所以我需要验证它有+44在一开始,包括+44,它是13位数长,我发现了一些不同的技术,但没有什么,只是一步一步定义它只是复制和粘贴,我想学习如何做,以便我知道未来的参考。
任何帮助都将不胜感激
谢谢
发布于 2011-06-04 23:30:56
虽然这可以通过PHP string functions简单地完成,但我强烈建议您利用这个机会学习regular expressions。准备好之后,就可以使用PHP PCRE functions来应用该正则表达式了。
注意:这个答案是根据OP请求故意推广的,目的是为了教人去钓鱼。我鼓励您在查看这些资源后,发布一个单独的、更具体的问题。
发布于 2011-06-04 23:53:43
简单的方法:
php代码:
if (isset($_POST['send'])) {
$mobile = $_POST['mobilenumber'];
// get the first 3 string
$begin = substr($mobile,0,3);
// get the rest of the posted string and add it to 0 to make it to number
// 'intval($variable)' and '(int) $variable' do the same
$theOthers = 0+substr($mobile,3);
// OR $theOthers = intval(substr($mobile,3));
// OR $theOthers = (int) substr($mobile,3);
$ok = true;
echo strlen($mobile);
// check the first 3 string
// if it's not equal with "+44", the entry is wrong
if ($begin != "+44") {
$ok = false;
} else {
// check the length of the input
// if it's not equal with 13, the entry is wrong
if (strlen($mobile)!=13) {
$ok = false;
}
}
if ($ok) {
// do something
}
}html代码:
<form method="post">
<input type="text" name="mobilenumber" maxlength="13" value="+44">
<input type="submit" name="send" value="Send">
</form>https://stackoverflow.com/questions/6237585
复制相似问题