答案 0 :(得分:1)
最后,我实现了自定义验证,以便在一个文本框中实现手机号码和电子邮件ID验证。
示例代码
public class customvalidation : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
string email = value.ToString();
if (Regex.IsMatch(email, @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", RegexOptions.IgnoreCase))
{
return ValidationResult.Success;
}
else if (Regex.IsMatch(email, @"(\d*-)?\d{10}", RegexOptions.IgnoreCase))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult("Invalid EmailID or Mobile Number");
}
}
else
{
return new ValidationResult("" + validationContext.DisplayName + "is required");
}
//return base.IsValid(value, validationContext);
}
}
答案 1 :(得分:0)
您可以执行类似于文本框中第一个按键事件的操作,您可以检查它是以数字还是字符开头,根据您可以验证其电子邮件ID或电话号码的结果
$("#TextboxID").keyup(function(){
var value=$("#TextboxID").val();
//Regex for mobile number
var phoneNumber = ^[0-9]{1};
//Regex for Email Id
var Email= ^[a-zA-Z]{1};
//Now in if condition you can check whether it begins with a number or a character.
});