我會嘗試以下方法。我無法分辨你是否需要帳號只能是數字或只有字母,所以我任意決定你只希望它只是數字。
編輯,以匹配您的意見*
check_num_compute(NumeroCompte) when length(NumeroCompte) == 9->
case io_lib:printable_unicode_list(NumeroCompte) of
true -> validate_contents(NumeroCompte);
false -> {error, not_string_of_numbers}
end;
check_num_compute(NumeroCompte) when is_list(NumeroCompte) ->
{error, wrong_length};
check_num_compute(_) ->
{error, not_string}.
validate_contents(NumeroCompte)->
AcceptFn = fun(C)->C >= $0 andalso C =< $9 end,
case lists:dropwhile(AcceptFn, NumeroCompte) of
[] -> true;
_ -> {error, not_all_numbers}
end.
19> t:check_num_compute([1,2,3,4,5,6,7,8,9]).
{error,not_string_of_numbers}
20> t:check_num_compute("123456789").
true
21> t:check_num_compute([1,2,3,4,5,6,7,8,9]).
{error,not_string_of_numbers}
23> t:check_num_compute("12345678f").
{error,not_all_numbers}
25> t:check_num_compute([]).
{error,wrong_length}
如果你想要的帳號是字母,不是一個簡單的改變validate_contents/1應該足夠了。
而且,你可能更喜歡下面的列表:dropwhile/2做法:
validate_contents([]) ->
true;
validate_contents([C|Cs]) when C >= $0, C =< $9 ->
validate_contents(Cs);
validate_contents(_) ->
{error, bad_arg}.
來源
2012-12-12 21:51:04
Jr0
我編輯我的答案,以更好地匹配您所需的錯誤消息,見下面 – Jr0