I tried the following code for digits-only validation for a contact number validation in Mvc web app.
[RegularExpression(#"/(^\(\d{10})?)$/", ErrorMessage = "Please enter proper contact details.")]
[Required]
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
But the validation expression is not working.
For the contact number I want to only accept digits. It can be either a 10 digit mobile number or a land-line number.
If don't have any restrictions other than numbers only, this should fit:
[RegularExpression(#"^\d+$", ErrorMessage = "Please enter proper contact details.")]
[Required]
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
/ / is javascript way to build a regular expression literal object. In .NET you should not use it.
Try the following:
#"^\((\d{10}?)\)$"
or if you want exactly 10 digits:
#"^(\d{10})$"
This worked for me:
[RegularExpression(#"^[0-9]{10}", ErrorMessage = "Please enter proper contact details.")]
Related
I'm developing a MVC project, I want to validate it for some text filed are uppercase. How can I do it?
I'm using this **text-transform:uppercase** but I want to validate in model
[DisplayName("Last Name :")]
[Required]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Last Name must be between 3 and 50 characters!")]
public string LastName { get; set; }
[RegularExpression(#"[A-Z]{3,50}$",
ErrorMessage = "Only uppercase Characters are allowed.")]
use regular expression attribute.
I want to display two different error message. First give for required and second for Int64 value. Here is my code.
[Display(Name = "Employee")]
[Required]
public Int64 EmployeeId { get; set; }
You can use the RangeAttribute.
[Required(ErrorMessage = "Msg1")]
[Range(10, 1000,
ErrorMessage = "Value for {0} must be between {1} and {2}.")]
public object Weight;
You can use data annotation extentions here: http://dataannotationsextensions.org/
Then add the two annotations as follows:
[Required(ErrorMessage = "Employee Id is required")]
[DataAnnotationsExtensions.Integer(ErrorMessage = "Please enter a valid number.")]
public Int64 EmployeeId { get; set; }
I am using ASP.NET MVC 4 and I am looking for an attribute for SSN Validation.
[Required(ErrorMessage = "Social Security is Required")]
[SSN]
public string SSN { get; set; }
I know this doesn't work, but this is what I am looking for.
Can anyone Help
You need to use a Regex.
Try something like this.
[Required(ErrorMessage = "SSN is Required")]
[RegularExpression(#"^\d{9}|\d{3}-\d{2}-\d{4}$", ErrorMessage = "Invalid Social Security Number")]
public string SSN { get; set; }
You need to use a Regex.
[RegularExpression(#"[1-9]{1}\d{2}-\d{2}-\d{4}$", ErrorMessage = "Invalid Social Security Number")]
This Regex also check's that the first digit must not be zero.
In my form I have phone and mobile number fields I need to guarantee that the user enters at least one of the numbers. I don’t want to force the user to enter both numbers.
I looked at this previous post but it didn’t work for me
conditional either or validation in asp.net mvc2
public class ContractViewModel
{
[Required(ErrorMessage = "Please enter First Name")]
[StringLength(50, ErrorMessage = "Length of First Name must be less than 50")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter Last Name")]
[StringLength(50, ErrorMessage = "Length of Last Name must be less than 50")]
public string LastName { get; set; }
[Required(ErrorMessage = "Please enter Phone Number")]
[StringLength(10,ErrorMessage = "Length of Phone No. must be less than 10")]
[RegularExpression("^[0-9]{10}$", ErrorMessage = "Please enter a valid home phone")]
public string Phone { get; set; }
[Required(ErrorMessage = "Please enter Mobile No.")]
[StringLength(10, ErrorMessage = "Length of Mobile No. must be les than 10")]
[RegularExpression("^[0-9]{10}$", ErrorMessage = "Please enter a valid mobile")]
public string Mobile { get; set; }
}
How would I go about doing this?
The article here mentions the use of Data Annotations Model Binder that is available here.
Does anyone know what it's intent is? To do DA validation, I don't need a special Model binder in MVC 2.0
The first release of ASP.Net MVC didn't support validation via Data Annotations as part of the framework. The intent of the linked codeplex code was to specifically allow usage of attribute oriented validation (which was in high demand) as a stopgap to the solution that was implemented in the second release.
DataType
Specify the datatype of a property
DisplayName
specify the display name for a property.
DisplayFormat
specify the display format for a property like different format for Date proerty.
Required
Specify a property as required.
ReqularExpression
validate the value of a property by specified regular expression pattern.
Range
validate the value of a property with in a specified range of values.
StringLength
specify min and max length for a string property.
MaxLength
specify max length for a string property.
Bind
specify fields to include or exclude when adding parameter or form values to model properties.
ScaffoldColumn
specify fields for hiding from editor forms.
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Employee.Models
{
[Bind(Exclude = "EmpId")]
public class Employee
{
[ScaffoldColumn(false)]
public int EmpId { get; set; }
[DisplayName("Employee Name")]
[Required(ErrorMessage = "Employee Name is required")]
[StringLength(100,MinimumLength=3)]
public String EmpName { get; set; }
[Required(ErrorMessage = "Employee Address is required")]
[StringLength(300)]
public string Address { get; set; }
[Required(ErrorMessage = "Salary is required")]
[Range(3000, 10000000,ErrorMessage = "Salary must be between 3000 and 10000000")]
public int Salary{ get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
[MaxLength(50)]
[RegularExpression(#"[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
public string Email { get; set; }
}
}