关于c语言判断电话号码合法问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于c语言判断电话号码合法问题相关的知识,希望对你有一定的参考价值。
我要输入一个电话号码char phone[15];
输入合法数据为数字和-,类似123-123456
请问我怎么判断输入的数据是否合法,
inputTel:
printf("Please enter the engineer's telephone:");
scanf("%s",&Engineer->EngineerPhone);
if(sizeof(&Engineer->EngineerPhone)<=0||sizeof(&Engineer->EngineerPhone)>15)
printf("Error! please re-enter.\n");
goto inputTel;
for(x=0;x<sizeof(&Engineer->EngineerPhone);x++)
if(((&Engineer->EngineerPhone<'0')||(&Engineer->EngineerPhone>'9'))&&((&Engineer->EngineerPhone)!='-'))
printf("Error! please re-enter.\n");
goto rinputel;
你们看看这段代码怎么改呗 谢谢了
不过正则表达式有一些复杂,很难马上上手。
可以看看这里:http://www.2cto.com/kf/201110/107088.html 参考技术A /* 检查电话合法性的代码 */
bool check_phone_num_correctness(const char* buffer)
const char* p = buffer;
for(; *p && (isdigit(*p) || *p == '-'); ++p);
return (*p == NULL) ? true : false;
bool get_phone_number(char* buffer, int max_size)
/* 使用一个更大的缓冲区保存输入, 如果字符串太长了, 不可以拷贝到你设定的 buffer[15]中 */
char big_buffer[1000];
printf("Please enter the engineer's telephone:");
scanf("%s", big_buffer);
int phone_size = strlen(big_buffer);
/* 判断是否合法, 1) 长度不超过buffer的长度 2) 字符内容符合电话号码要求(自己可以再扩展) */
bool is_size_correct = (phone_size > 0) && (phone_size < max_size);
if(!is_size_correct || !check_phone_num_correctness(big_buffer))
fprintf(stderr, "Error! please re-enter.\n");
return false;
/* copy back */
strcpy(buffer, big_buffer);
return true;
/* calling method */
char phone[15];
bool phone_correct = get_phone_number(phone, 15); 参考技术B bool bRightPhone = false;
while(!bRightPhone)
printf("Please enter the engineer's telephone:");
scanf("%s",&Engineer->EngineerPhone);
bool bEveryRight = true;
if(sizeof(&Engineer->EngineerPhone) > 0 && sizeof(&Engineer->EngineerPhone) > 15)
for(x=0;x<sizeof(&Engineer->EngineerPhone);x++)
if(((&Engineer->EngineerPhone < '0') && (&Engineer->EngineerPhone > '9')) && ((&Engineer->EngineerPhone) !='-'))
bEveryRight = false;
break;
else
bEveryRight = false;
bRightPhone = bEveryRight ;
if(!bRightPone)
printf("Wrong PhoneNumber,Again!\n");
本回答被提问者采纳 参考技术C 电话格式有几种?如果就只有上面的格式,你只需要检查前三个和后6个是否为数字,第4个是否为'-' 参考技术D 不好意思,弄说错了
oracle判断身份证号码是否合法(包含15位身份证)
CREATE OR REPLACE FUNCTION Func_checkidcard (p_idcard IN VARCHAR2) RETURN INT
IS
v_regstr VARCHAR2 (2000);
v_sum NUMBER;
v_mod NUMBER;
v_checkcode CHAR (11) := '10X98765432';
v_checkbit CHAR (1);
v_areacode VARCHAR2 (2000) := '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,';
BEGIN
CASE LENGTHB (p_idcard)
WHEN 15
THEN -- 15位
IF INSTRB (v_areacode, SUBSTR (p_idcard, 1, 2) || ',') = 0 THEN
RETURN 0;
END IF;
IF MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 2)) + 1900, 400) = 0
OR
(
MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 2)) + 1900, 100) <> 0
AND
MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 2)) + 1900, 4) = 0
)
THEN -- 闰年
v_regstr :=
'^[1-9][0-9]5[0-9]2((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]3$';
ELSE
v_regstr :=
'^[1-9][0-9]5[0-9]2((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]3$';
END IF;
IF REGEXP_LIKE (p_idcard, v_regstr) THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
WHEN 18
THEN -- 18位
IF INSTRB (v_areacode, SUBSTRB (p_idcard, 1, 2) || ',') = 0 THEN
RETURN 0;
END IF;
IF MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 4)), 400) = 0
OR
(
MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 4)), 100) <> 0
AND
MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 4)), 4) = 0
)
THEN -- 闰年
v_regstr :=
'^[1-9][0-9]5(19|20)[0-9]2((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]3[0-9Xx]$';
ELSE
v_regstr :=
'^[1-9][0-9]5(19|20)[0-9]2((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]3[0-9Xx]$';
END IF;
IF REGEXP_LIKE (p_idcard, v_regstr) THEN
v_sum :=
( TO_NUMBER (SUBSTRB (p_idcard, 1, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 11, 1))
)
* 7
+ ( TO_NUMBER (SUBSTRB (p_idcard, 2, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 12, 1))
)
* 9
+ ( TO_NUMBER (SUBSTRB (p_idcard, 3, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 13, 1))
)
* 10
+ ( TO_NUMBER (SUBSTRB (p_idcard, 4, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 14, 1))
)
* 5
+ ( TO_NUMBER (SUBSTRB (p_idcard, 5, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 15, 1))
)
* 8
+ ( TO_NUMBER (SUBSTRB (p_idcard, 6, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 16, 1))
)
* 4
+ ( TO_NUMBER (SUBSTRB (p_idcard, 7, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 17, 1))
)
* 2
+ TO_NUMBER (SUBSTRB (p_idcard, 8, 1)) * 1
+ TO_NUMBER (SUBSTRB (p_idcard, 9, 1)) * 6
+ TO_NUMBER (SUBSTRB (p_idcard, 10, 1)) * 3;
v_mod := MOD (v_sum, 11);
v_checkbit := SUBSTRB (v_checkcode, v_mod + 1, 1);
IF v_checkbit = upper(substrb(p_idcard,18,1)) THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
ELSE
RETURN 0;
END IF;
ELSE
RETURN 0; -- 身份证号码位数不对
END CASE;
EXCEPTION
WHEN OTHERS
THEN
RETURN 0;
END Func_checkidcard;
以上是关于关于c语言判断电话号码合法问题的主要内容,如果未能解决你的问题,请参考以下文章
急求一个JAVA中判断用户输入的数字是一个合法的电话号码的正则表达式,包括住宅电话与移动电话。