Hi,
I am trying to validate a simple form using jQuery.
So,I created database company and in that my table manager.
here I am trying to add manager record.
the code is here-
AddManager.aspx file
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Formvalidation.Models.manager>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
AddManager
</asp:Content>
<asp:Content ContentPlaceHolderID ="HeadContent" runat ="server" >
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript" src="../../Scripts/jquery.js">
$.validator.addMethod('m_number', function (value) {
return /(^9[0-9]{9})$/.test(value);
}, 'Please enter a valid US or Canadian postal code.');
$(document ).ready(function(){
$("form").validate({
rules:
{
rules:{
m_id:{
required:true,
minlength:2,
number:true
},
m_name:{
required:true,
minLength:5
},
m_email:{
required:true,
email:true
},
m_number:{
required:true,
number:true,
regex:"^9[0-9]{9}"
},
m_bdate:{
required:true,
date:true
}
},
messages:{
m_id:{
required:"Please enter a Id",
minLength:"your Id must be 2 digit long"
},
m_name:{
required:"Please provide a name"
},
m_email:{
required:"Please provide a valid e_mail id",
},
m_number: "Please enter a valid contact number",
m_bdate: "Please enter valid date"
}
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>AddManager</h2>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="m_id">m_id:</label>
<%= Html.TextBox("m_id") %>
<%= Html.ValidationMessage("m_id", "*") %>
</p>
<p>
<label for="m_name">m_name:</label>
<%= Html.TextBox("m_name") %>
<%= Html.ValidationMessage("m_name", "*") %>
</p>
<p>
<label for="m_email">m_email:</label>
<%= Html.TextBox("m_email") %>
<%= Html.ValidationMessage("m_email", "*") %>
</p>
<p>
<label for="m_number">m_number:</label>
<%= Html.TextBox("m_number") %>
<%= Html.ValidationMessage("m_number", "*") %>
</p>
<p>
<label for="m_bdate">m_bdate:</label>
<%= Html.TextBox("m_bdate") %>
<%= Html.ValidationMessage("m_bdate", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
I have included jQuery.js file and jQuery.validate.js file into this application and give refernce to it.
but not a single validation is working here..
whats the reason..?
where i made mistake..? plz help.:-O
I am trying with data annotation validators,
I check it on employee table with entity framework so , I wrote an employee.cs file -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace Formvalidation.Models
{
[MetadataType(typeof(employeeMetadata))]
public partial class employee
{
}
public class employeeMetadata
{ [Required]
[DisplayName ("Employee ID")]
public object e_id { get; set; }
[Required]
[DisplayName (" Employee Name")]
[StringLength(15)]
[RegularExpression("[a-zA-Z]{5-15}", ErrorMessage = "Entar charachters only")]
public object e_name { get; set; }
[Required]
[DisplayName("City")]
public object e_city { get; set; }
[Required]
[DisplayName("Date of Birth")]
[DataType(DataType.Date,ErrorMessage="Entar valid date")]
public object e_bdate { get; set; }
[Required]
[DisplayName("Contact Number")]
[RegularExpression("^9[0-9]{8}$",ErrorMessage="Entar valid Mobile Number")]
public object e_contact { get; set; }
[Required]
[DisplayName("Salary")]
[Range (100,500000,ErrorMessage ="Entar valid Salary")]
public object e_salary { get; set; }
[Required ]
[DisplayName("E-mail ID")]
[DataType(DataType.EmailAddress,ErrorMessage="Entar Valid Email ID" )]
public object e_mailID { get; set; }
}
}
I want to restrict numbers in emp_name field, but Its not working using regular expression also,
whats going wrong here?