Thursday 9 July 2015

basic jquery validation for MVC

@model MvcApplication1.ViewModel.EMPVM





@{
ViewBag.Title = "Create";






}
<h2>Create</h2>

@using (Html.BeginForm()) {






@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>


<legend>EMPVM</legend>

<div class="editor-label">





@Html.LabelFor(model => model.EID)
</div>


<div class="editor-field">





@Html.EditorFor(model => model.EID)


@Html.ValidationMessageFor(model => model.EID)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.NAME)
</div>


<div class="editor-field">





@Html.EditorFor(model => model.NAME)


@Html.ValidationMessageFor(model => model.NAME)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.ADDRESS)
</div>


<div class="editor-field">





@Html.EditorFor(model => model.ADDRESS)


@Html.ValidationMessageFor(model => model.ADDRESS)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.GENDER)
</div>


<div class="editor-field">


@Html.RadioButtonFor(m=>m.GENDER,"Male")Male

&nbsp;@Html.RadioButtonFor(m => m.GENDER, "Female")Female






@Html.ValidationMessageFor(model => model.GENDER)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.PASSWORD)
</div>


<div class="editor-field">





@Html.EditorFor(model => model.PASSWORD)


@Html.ValidationMessageFor(model => model.PASSWORD)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.CPASSWORD)
</div>


<div class="editor-field">





@Html.EditorFor(model => model.CPASSWORD)


@Html.ValidationMessageFor(model => model.CPASSWORD)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.DOB)
</div>


<div class="editor-field">





@Html.EditorFor(model => model.DOB)


@Html.ValidationMessageFor(model => model.DOB)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.HOBBY)
</div>


<div class="editor-field">


@for (int i = 0; i < Model.HOBBY.Count; i++)






{


@Html.HiddenFor(m=>m.HOBBY[i].HID)
@Html.CheckBoxFor(m => m.HOBBY[i].ISACTIVE.Value, new {@class="cb" })






@Html.DisplayFor(m=>m.HOBBY[i].HNAME)


}


@Html.ValidationMessageFor(model => model.DHOBBY)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.INTEREST)
</div>


<div class="editor-field">


@Html.ListBoxFor(model => model.SINTEREST, new MultiSelectList(Model.INTEREST, "IID", "INAME"), new { size = 3, style = "width:200px" })






@Html.ValidationMessageFor(model => model.DINTEREST)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.QUESTION)
</div>


<div class="editor-field">


@Html.DropDownListFor(m => m.SQUESTION, new SelectList(Model.QUESTION, "QID", "QNAME"), "Select", new {style="width:200px" })






@Html.ValidationMessageFor(model => model.SQUESTION)
</div>






<div class="editor-label">





@Html.LabelFor(model => model.EMAIL)
</div>


<div class="editor-field">





@Html.EditorFor(model => model.EMAIL)


@Html.ValidationMessageFor(model => model.EMAIL)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.SALARY)
</div>


<div class="editor-field">





@Html.EditorFor(model => model.SALARY)


@Html.ValidationMessageFor(model => model.SALARY)
</div>

<div class="editor-label">





@Html.LabelFor(model => model.PHOTE)
</div>


<div class="editor-field">


@Html.TextBoxFor(model => model.PHOTE, new {type="file" })






@Html.ValidationMessageFor(model => model.DPHOTE)
</div>






<p>


<input type="submit" id="btnCreate" value="Create" />






</p>


</fieldset>





}
<div>


@Html.ActionLink("Back to List", "Index")


</div>





@section Scripts {
@Scripts.Render("~/bundles/jqueryval")


<script type="text/javascript">


$(function ()






{
$('#btnCreate').click(function ()






{
if ($('#EID').val() == "")






{
alert('Eid should not blank.')

$('#EID').focus();

return false;






}
else if ($('#NAME').val() == "") {

alert('Name should not blank.')

$('#NAME').focus();

return false;






}
else if ($('#ADDRESS').val() == "") {

alert('Address should not blank.')

$('#ADDRESS').focus();

return false;






}
else if ($(":radio[name='GENDER']").is(':checked') == false) {

alert('Select a gender.')

$('#GENDER').focus();

return false;






}
else if ($('#PASSWORD').val() == "") {

alert('Password should not blank.')

$('#PASSWORD').focus();

return false;






}
else if ($('#PASSWORD').val().length < 6 || $('#PASSWORD').val().length>8) {

alert('Password length between 6 to 8.')

$('#PASSWORD').focus();

return false;






}
else if ($('#CPASSWORD').val() == "") {

alert('Confirm password should not blank.')

$('#CPASSWORD').focus();

return false;






}
else if ($('#CPASSWORD').val().length < 6 || $('#CPASSWORD').val().length > 8) {

alert('Confirm password length between 6 to 8.')

$('#CPASSWORD').focus();

return false;






}
else if ($('#PASSWORD').val() != $('#CPASSWORD').val())






{
alert('Password and confirm password must be same.')

$('#CPASSWORD').focus();

return false;






}
else if ($('#DOB').val() == "") {

alert('DOB should not blank.')

$('#DOB').focus();

return false;






}
else if ($(".cb").is(':checked') ==false)






{
alert('Please select a hobby.')

$('#HOBBY').focus();

return false;






}
else if ($('#SINTEREST :selected').length == 0)






{
alert('Please select a interest.')

$('#SINTEREST').focus();

return false;







}
else if ($('#SQUESTION :selected').text()!="Select") {

alert('Please select a question.')

$('#SQUESTION').focus();

return false;






}
else if ($('#EMAIL').val() == "")






{
alert('Email should not blank.')

$('#EMAIL').focus();

return false;






}
else if ($('#SALARY').val() == "") {

alert('Salary should not blank.')

$('#SALARY').focus();

return false;






}
else if ($('#PHOTE').val() == "") {

alert('Phote should not blank.')

$('#PHOTE').focus();

return false;






}


});
$('#EMAIL').blur(function () {

echeck($(this).val());

$(this).focus();






});
$('#SALARY').keyup(function ()






{
var m = "0123456789.";

var n = $(this).val();

for(var i=0;i<n.length;i++)

if (m.indexOf(n.charAt(i)) < 0)






{
alert('You can enter only number.')

$(this).focus();

return false;






}


});
function echeck(str) {






 
var at = "@@"

var dot = "."

var lat = str.indexOf(at)

var lstr = str.length

var ldot = str.indexOf(dot)

if (str.indexOf(at) == -1) {

alert("Invalid E-mail ID")

return false





}


 
if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {

alert("Invalid E-mail ID")

return false





}


 
if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {

alert("Invalid E-mail ID")

return false





}


 
if (str.indexOf(at, (lat + 1)) != -1) {

alert("Invalid E-mail ID")

return false





}


 
if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {

alert("Invalid E-mail ID")

return false





}


 
if (str.indexOf(dot, (lat + 2)) == -1) {

alert("Invalid E-mail ID")

return false





}


 
if (str.indexOf(" ") != -1) {

alert("Invalid E-mail ID")

return false





}


 
return true





}


 


});
//check dropdown and select a value in jquery//

if ($('#AID>option').length == 2)
$('#AID').prop('selectedIndex', 1);

    </script>





}

No comments:

Post a Comment