检查Email地址的合法输入[几乎可以用于实现开发当中]
2012-05-10 08:50
441 查看
#include <iostream> using namespace std; const int INT_ZERO = 0; const int INDEX_ZERO = 0; const int INT_ONE = 1; const int INDEX_ONE = 1; const int HAS_NO_ELEMENT = 0; const int MAX_LENGTH = 30; const char NULL_TERMINATED = '\0'; const char CHAR_AT = '@'; const char CHAR_POINT = '.'; const char CHAR_LINE = '-'; const char CHAR_UNDER_LINE = '_'; //自定义一些Email的合法字符 bool isAllowChar( char ch ) { if (ch == CHAR_AT || ch == CHAR_POINT || ch == CHAR_LINE || ch == CHAR_UNDER_LINE) { return true; } else { return false; } } //dizly@qq.com bool isEmail( char *str ) { if (NULL == str) { cout << "参数不能指向空的地址!" << endl; throw; } if (MAX_LENGTH < strlen( str )) { cout << "Email不能超过" << MAX_LENGTH << "个字符!" << endl; system( "PAUSE" ); throw; } //'@'或'.'不能出现在第一个字符的位置上 if (CHAR_AT == *str || CHAR_POINT == *str) { return false; } int countCharAt = INT_ZERO; int countCharPoint = INT_ZERO; while (NULL_TERMINATED != *str) { if (!isalnum( *str ) && !isAllowChar( *str )) //合法的字符应该是:数字、字母以及本程序另外允许的字符 { return false; } if (CHAR_AT == *str) { ++countCharAt; } if (CHAR_POINT == *str) { ++countCharPoint; } //'@'和'.'不能相邻 if ((CHAR_AT == *str && CHAR_POINT == *(str+INDEX_ONE)) || (CHAR_POINT == *str && CHAR_AT == *(str+INDEX_ONE))) { return false; } ++str; } //'@'或'.'不能出现在最后一个字符的位置上 if (CHAR_AT == *(str - INDEX_ONE) || CHAR_POINT == *(str - INDEX_ONE)) { return false; } if (INT_ONE != countCharAt || INT_ONE != countCharPoint) //一个Email有且仅有一个'@'和'.' { return false; } return true;//程序能够执行到这里,表示所设的关卡都通过了 } void testFunc( char *str ) { if (NULL == str) { throw; } if (isEmail( str )) { cout << str << " [合法]" << endl; } else { cout << str << " [不合法]" << endl; } } int main( void ) { testFunc( "c_p-p@ok2002.com" ); testFunc( "123456789@ok2002.com" ); testFunc( "abcdefghi@ok2002.com" ); testFunc( "jklmnopqr@ok2002.com" ); testFunc( "stuvwxyz@ok2002.com" ); testFunc( "ABCDEFGHI@ok2002.com" ); testFunc( "JKLMNOPQR@ok2002.com" ); testFunc( "STUVWXYZ@ok2002.com" ); testFunc( "@cppok2002.com" ); //'@'作首字符 testFunc( ".cpp@ok2002com" ); //'.'作首字符 testFunc( "cpp@ok@2002.com" ); //两个'@' testFunc( "cpp@ok.2002.com" ); //两个'.' testFunc( "cppok2002.com@" ); //'@'作尾字符 testFunc( "cpp@ok2002com." ); //'.'作首字符 testFunc( "cpp@.ok2002com" ); //'@'和'.'相邻 testFunc( "cpp.@ok2002com" ); //'@'和'.'相邻 testFunc( "cpp@ok*2002.com" ); //含有非法字符 testFunc( "cpp@ok'2002.com" ); //含有非法字符 testFunc( "cpp@ok+2002.com" ); //含有非法字符 testFunc( "cpp@ok#2002.com" ); //含有非法字符 testFunc( "cpp@ok/2002.com" ); //含有非法字符 testFunc( "cpp@ok|2002.com" ); //含有非法字符 testFunc( "cpp@ok\2002.com" ); //含有非法字符 testFunc( "cpp@ok?2002.com" ); //含有非法字符 testFunc( "cpp@ok$2002.com" ); //含有非法字符 testFunc( "cpp@ok^2002.com" ); //含有非法字符 testFunc( "cpp@ok%2002.com" ); //含有非法字符 testFunc( "cpp@ok&2002.com" ); //含有非法字符 testFunc( "cpp@ok=2002.com" ); //含有非法字符 testFunc( "cpp@ok(2002.com" ); //含有非法字符 testFunc( "cpp@ok)2002.com" ); //含有非法字符 testFunc( "cpp@ok[2002.com" ); //含有非法字符 testFunc( "cpp@ok]2002.com" ); //含有非法字符 testFunc( "cpp@ok,2002.com" ); //含有非法字符 testFunc( "cpp@ok;2002.com" ); //含有非法字符 testFunc( "cpp@ok:2002.com" ); //含有非法字符 testFunc( "cpp@ok<2002.com" ); //含有非法字符 testFunc( "cpp@ok>2002.com" ); //含有非法字符 testFunc( "cpp@ok!2002.com" ); //含有非法字符 testFunc( "cppok2002.com" ); //缺少'@' testFunc( "cpp@ok2002com" ); //缺少'.' testFunc( "cpp@ok2002.comcpp@ok2002.comcpp@ok2002.comcpp@ok2002.com" ); //太长了 system( "PAUSE" ); return EXIT_SUCCESS; } /*----------- c_p-p@ok2002.com [合法] 123456789@ok2002.com [合法] abcdefghi@ok2002.com [合法] jklmnopqr@ok2002.com [合法] stuvwxyz@ok2002.com [合法] ABCDEFGHI@ok2002.com [合法] JKLMNOPQR@ok2002.com [合法] STUVWXYZ@ok2002.com [合法] @cppok2002.com [不合法] .cpp@ok2002com [不合法] cpp@ok@2002.com [不合法] cpp@ok.2002.com [不合法] cppok2002.com@ [不合法] cpp@ok2002com. [不合法] cpp@.ok2002com [不合法] cpp.@ok2002com [不合法] cpp@ok*2002.com [不合法] cpp@ok'2002.com [不合法] cpp@ok+2002.com [不合法] cpp@ok#2002.com [不合法] cpp@ok/2002.com [不合法] cpp@ok|2002.com [不合法] cpp@ok2.com [不合法] cpp@ok?2002.com [不合法] cpp@ok$2002.com [不合法] cpp@ok^2002.com [不合法] cpp@ok%2002.com [不合法] cpp@ok&2002.com [不合法] cpp@ok=2002.com [不合法] cpp@ok(2002.com [不合法] cpp@ok)2002.com [不合法] cpp@ok[2002.com [不合法] cpp@ok]2002.com [不合法] cpp@ok,2002.com [不合法] cpp@ok;2002.com [不合法] cpp@ok:2002.com [不合法] cpp@ok<2002.com [不合法] cpp@ok>2002.com [不合法] cpp@ok!2002.com [不合法] cppok2002.com [不合法] cpp@ok2002com [不合法] Email不能超过30个字符! 请按任意键继续. . . --------------------------*/
相关文章推荐
- [置顶] Android 百度地图开发--- 导航功能输入起始地址实现导航,地址解析与反解析的使用
- 判断输入的EMAIL地址是否合法
- 如何实现在内网输入公网IP地址可以直接访问内网服务器
- 正则表达式检测用户输入的email地址是否合法
- 正则表达式简单的检查输入email是否合法程序
- 正则表达式检测用户输入的email地址是否合法
- iPhone开发之判断用户输入的邮箱地址是否合法的代码
- 判断输入的EMAIL地址是否合法
- 用 scanf 系列函数实现简单的 email 地址合法性检查功能
- 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能
- 检查输入的邮件地址是否合法
- android 新建/编辑联系人时,检测输入的email/邮件地址格式是否合法
- Python 最近因开发项目的需要,有一个需求,就是很多SNS网站都有的通过 Email地址 导入好友列表,不过这次要导入的不是Email 列表,而是QQ的好友列表。 实现方式: 通过goog
- 利用利用String类或StringBuffer类的方法,对输入的Email地址进行有效性验证。
- 利用String类或StringBuffer类的方法,对输入的Email地址进行有效性验证。
- 利用String类或StringBuffer类的方法,对输入的Email地址进行有效性验证。提示: 1)Email地址中应包含“@”和“.”符号; 2)“@”符号应该在“.”符号之前; 3
- 简化 RecyclerView 的多种 type 的 adapter,Fragment 可以动态添加到 RecyclerView 上,实现复杂的界面分多个模块开发
- struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
- 通过dsa认证可以实现在localbox无需输入密码来登陆外部主机
- android开发-验证邮箱输入是否合法