Introduction:
Here I will explain how to implement model validations using data annotations in asp.net mvc with example or add validation to model in asp.net mvc with examples or asp.net mvc validate model properties / attributes using data annotations with example or asp.net mvc model validation to allow only alphabets in textbox with example. In asp.net mvc we can easily implement validation on model properties by using DataAnnotations properties.
Description:
In previous articles I explained asp.net mvc compare model properties with example, jQuery Google Pie chart in asp.net with database, sql server get string before and after with example, sql query to get data month wise and year wise with example, sql query to exclude weekends with example, asp.net mvc crud operations using entity framework, Use mysql database in asp.net with examples and many articles relating to asp.net mvc , asp.net, c#,vb.net . Now I will explain how to implement model validations using data annotations in asp.net mvc with example.
In previous articles I explained asp.net mvc compare model properties with example, jQuery Google Pie chart in asp.net with database, sql server get string before and after with example, sql query to get data month wise and year wise with example, sql query to exclude weekends with example, asp.net mvc crud operations using entity framework, Use mysql database in asp.net with examples and many articles relating to asp.net mvc , asp.net, c#,vb.net . Now I will explain how to implement model validations using data annotations in asp.net mvc with example.
Generally
in
asp.net
mvc
we
can easily implement validations to model properties by using DataAnnotations.
We will see how to implement validation to model properties with example for
that
create
asp.net
mvc
application and Open visual studio
à Go to File à Select New à Project like as shown below
|
Once we select Project new popup will open in
that select Asp.Net Web Application and give name to application and
click OK like as shown below
|
Once click OK new popup will open in that
select Empty template and select
folders and core reference as MVC
and click OK like as shown below
|
Once we finished creating application our
project structure will be like as shown below
|
Now right click on your Models folder à
select Add
à
select Class
like as shown below.
Once we add new model (UsersModel) open it and write code like as shown below.
using
System.ComponentModel.DataAnnotations;
namespace MVCExamples.Models
{
public
class UsersModel
{
[Key]
public
int UserId { get
;
set; }
[RegularExpression("^[a-zA-Z]*$", ErrorMessage = "Only Alphabets are Allowed")]
[Required(ErrorMessage="Please Enter Username")]
public
string UserName { get
;
set; }
[Required(ErrorMessage="Please Enter Password")]
public
string Password { get
;
set; }
[Required(ErrorMessage="Please Enter Confirm Password")]
[Compare("Password",ErrorMessage=
"Both Password and Confirm Password Must be
Same"
)]
public
string ConfirmPassword { get; set; }
[Required(ErrorMessage="Please Enter Location")]
public
string Location { get
;
set; }
}
}
|
If you observe above code we added
DataAnnotations reference and used validation properties
Required,
Compare
,
RegularExpression
properties to compare
multiple properties and allow only alphabets in username field, etc. based on
our requirements.
Now add new controller for that right click on
your Controllers folder
à select Add à
select Controller like
as shown below.
Once we click on
Controller new popup will open in that select MVC 5 Controller - Empty and click Add like as shown below.
Once click on Add new
window will open in that give name of controller and click Add button then new
controller file will add to folder. Now open new controller (UserController) and write the code like
as shown below
using System.Web.Mvc;
namespace MVCExamples.Controllers
{
public class UserController : Controller
{
// GET: User
public ActionResult
Index()
{
return View();
}
public ActionResult
UserRegistration()
{
return View();
}
}
}
|
Now right click on UserRegistration method and select
Add
View
like as shown below
Once click Add View new
template will open in that select Template type “Create” and Model class as our “UsersModel” and click Add like as shown below.
Once we add view that will be like as shown below.
Now run your application and check the output that will be like as
shown below
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
2 comments :
Can you attach source code?
function AllowAlphabet(e) {
isIE = document.all ? 1 : 0
keyEntry = !isIE ? e.which : event.keyCode;
if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
return true;
else {
alert('Please Enter Only Character values.');
return false;
}
}
this is javascript code
Note: Only a member of this blog may post a comment.