2011-06-04 62 views
1

道歉打扰你,可能显得相当生硬的一些你一个问题,但我想知道如果你们能对事物的验证方面提供一些线索手机验证 - PHP的

Ive得到一个文本字段,我需要在手机号码上进行验证,所以我需要验证它在开头是否包含+44,并且包含+44这是一个13位数字,我发现了一些不同的技术,但没有定义它的一步一步只是只是复制和粘贴,我喜欢学习如何做,所以我知道将来的参考。

任何帮助,将不胜感激

感谢

回答

1

虽然这可以简单地PHP string functions做,我会劝你借此机会学习regular expressions。一旦准备就绪,您可以使用PHP PCRE functions来应用该正则表达式。

注:这个答案是在教一个人的兴趣有意推广到鱼,每OP请求。我鼓励在查看这些资源后发布一个单独的,更具体的问题。

0

简单的方法:

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> 
+1

'0 +'string''?为什么不使用'(int)'cast或'intval()'? – ThiefMaster 2011-06-04 16:07:46

+0

当然你可以做到。结果将是相同的。我有时候会用这个。 – FoPi 2011-06-04 20:43:19