手机号码校验

时间:2014-09-28 17:04:04   收藏:0   阅读:294
// temp.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

//手机号码校验
/*
 * 正则表达式:/^1[3548][0-9]{9}$/
 */
template <typename T> //T ~ (char*) | (string)
inline bool validateMobile(T pchMobile)
{
    int i=0;
    for(;i<12 && pchMobile[i]; i++)
    {
        if(i==0 && pchMobile[i] != 1)
            return false;
        else if(i==1 && (  pchMobile[i]!=3
                        && pchMobile[i]!=4
                        && pchMobile[i]!=5
                        && pchMobile[i]!=8))
            return false;
        else if(pchMobile[i]<0 || pchMobile[i]>9)
            return false;
    }
    return (i==11);//手机号码为11位
}

int _tmain(int argc, _TCHAR* argv[])
{
    string mobile("13714371213");
    bool ret=validateMobile(mobile);
    cout<<ret;
    getchar();
    return 0;
}

 

原文:http://www.cnblogs.com/liyou-blog/p/3998357.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!