首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >手机验证- Php

手机验证- Php
EN

Stack Overflow用户
提问于 2011-06-04 23:24:48
回答 2查看 1.4K关注 0票数 1

很抱歉用一个对你们中的一些人来说似乎相当直率的问题来打扰你们,但我想知道你们中是否有人可以在验证方面提供一些启发

我有一个文本域,我需要对一个手机号码进行验证,所以我需要验证它有+44在一开始,包括+44,它是13位数长,我发现了一些不同的技术,但没有什么,只是一步一步定义它只是复制和粘贴,我想学习如何做,以便我知道未来的参考。

任何帮助都将不胜感激

谢谢

EN

回答 2

Stack Overflow用户

发布于 2011-06-04 23:30:56

虽然这可以通过PHP string functions简单地完成,但我强烈建议您利用这个机会学习regular expressions。准备好之后,就可以使用PHP PCRE functions来应用该正则表达式了。

注意:这个答案是根据OP请求故意推广的,目的是为了教人去钓鱼。我鼓励您在查看这些资源后,发布一个单独的、更具体的问题。

票数 1
EN

Stack Overflow用户

发布于 2011-06-04 23:53:43

简单的方法:

php代码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
<form method="post">
<input type="text" name="mobilenumber" maxlength="13" value="+44">
<input type="submit" name="send" value="Send">
</form>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6237585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档