Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In my project I have user name and password etc.. fields.
i don't know how to give client side validation.Please tell me if any one know .
I am using mvc3 razor .

Thanks.
Posted

try:

XML
<head>
    <title>jquery validate</title>
    <!-- javascript will come here -->
    <script src="../../Scripts/jquery-1.4.4.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
    <script type="text/javascript">
            $(function () {
                $("#userform").validate({
                    rules: {
                    FirstName: { required: true, maxlength: 64 },
                        LastName: { required: true, maxlength: 64 },
                        Email: { maxlength: 32,email:true, }
                    }
                });
            });
    </script>
</head>

XML
<body>
    <form id="userform" action="Home/Index" method="post">
    <div>
        <label for="FirstName">
            First name</label></div>
    <div>
        <input id="FirstName" name="FirstName" type="text" /></div>
    <div>
        <label for="LastName">
            Last name</label></div>
    <div>
        <input id="LastName" name="LastName" type="text" /></div>
    <div>
        <label for="Email">
            Email</label></div>
    <div>
        <input id="Email" name="Email" type="text" /></div>
    <div>
    <input type="submit" value="Create" /></div>
    </form>
</body>
 
Share this answer
 
Comments
sankar071237 2-Mar-12 2:30am    
how to give validation for the following code.


<fieldset>
Patient

<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.MiddleName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MiddleName)
@Html.ValidationMessageFor(model => model.MiddleName)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</fieldset>
member60 2-Mar-12 2:35am    
the code works for strongly typed view too, the thing is that inculde the jquery script files.
using unobtrusive javascript you can achieve this,have a look at the following article and try:
http://weblogs.asp.net/imranbaloch/archive/2011/04/30/eagerly-performing-asp-net-mvc-3-unobtrusive-client-side-validation.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900