我正在开展一个用户应该输入电话号码的小项目,所以我想使用“ClientSideValidations”gem来验证这些信息。
validates_format_of :telcasa, :celular, :tel_flia, :tel_trab, :tel_ref_2, :tel_ref_1,
length: { in: 10 },
:with => /\A(\d{10}|\(?\d{3}\)?[-. ]\d{3}[-.]\d{4})\z/,
:message => "Formato invalido"
但是,对于将要使用该项目的区域,我必须验证对应于区号的电话的三个第一个号码(“809”/“829”/“849”)。如何验证用户是否使用三个区号之一正确键入了电话号码?
答案 0 :(得分:1)
将/\A(\d{10}|\(?\d{3}\)?[-. ]\d{3}[-.]\d{4})\z/
更改为:
/\A(\(?(809|829|849)\)?[-. ]\d{3}[-.]\d{4})\z/
我冒昧地放弃了你匹配任何十位数字的部分 - 不确定它为什么存在或如何在你的环境中使用它。
答案 1 :(得分:0)
您可以编写一些自定义验证
#include <stdio.h>
struct entry
{
char word[15];
char definition[50];
};
struct entry dictionary[100] =
{ {"boy", "a boy " },
{"aardvark", "a burrowing African mammal" },
{"acumen", "mentally sharp; keen" },
{"addle", "to become confused" },
{"cat", "a cat" },
{"affix", "to append; attach" },
{"agar", "a jelly made from seaweed" },
{"ahoy", "a nautical call of greeting" },
{"aigrette", "an ornamental cluster of feathers" },
{"ajar", "partially opened" }
};
int main(void)
{
int i;
void dictionarySort(struct entry dictionary[]);
dictionarySort(dictionary);
for(i = 0; i < 10; ++i)
{
printf("%s\n", dictionary[i].word);
}
return 0;
}
void dictionarySort(struct entry dictionary[])
{
int i, k, j;
struct entry temp[100];
for(i = 0; i <= 9; ++i)
{
for( k = 0; dictionary[i].word[k] != '\0'; ++k)
{
if( (dictionary[i].word[k] > dictionary[i+1].word[k] ) )
{
temp[i] = dictionary[i];
dictionary[i] = dictionary[i+1];
dictionary[i+1] = temp[i];
}
}
}
}
或者,如果您愿意,可以在函数validate do
valid_phone_codes = [ "007", "042", ...]
valid_phone_codes.each do |valid_code|
# Also handle optional parenthesis
return true if self.phone_number.starts_with?(valid_code, "(#{valid_code})")
end
errors.add(:phone_numbers, "Must start with a valid country code (one of #{valid_phone_codes.join(', ')}")
false
end
中声明此代码,然后添加一行
def valid_country_codes