Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Thursday, 27 October 2016

BootStrap datepiker different format (dd/mm/yy OR YY OR MM or MM/YY etc)

@{
  ViewBag.Title = "datepicker";
}

<h2>datepicker</h2>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
  <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css" rel="stylesheet" />

  <script src="~/Content/js/jquery.min.js"></script>
  <script src="~/Content/js/bootstrap.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.min.js"></script>

  <style>
  .ui-datepicker-calendar {
  display: none;
  }


  </style>
  <script>
  $(document).ready(function () {

  $('#datePickerAll').datepicker({
  format: 'dd-mm-yyyy',
  autoclose: true
  });


  $("#datepickerYear").datepicker({
  format: " yyyy", // Notice the Extra space at the beginning
  viewMode: "years",
  minViewMode: "years",
  autoclose:true
  });

  $("#datePickerMM").datepicker({
  format: " mm", // Notice the Extra space at the beginning
  viewMode: "months",
  minViewMode: "months",
  autoclose: true
  });



  $("#datepickermmyyyy").datepicker({
  format: "mm-yyyy",
  viewMode: "months",
  minViewMode: "months",
  autoclose: true
  });


  });

  </script>

</head>
<body>
  <div class="row">
  <div class="col-md-6">
  <strong>dd/mm/yyyy</strong>
  <div class="input-group input-append date" id="datePickerAll">
  <input type="text" class="form-control" name="date" />
  <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
  </div>
  </div>

  <div class="col-md-6">
  <strong>yyyy</strong>

  <div class="input-group input-append date" id="datepickerYear">
  <input type="text" class="form-control" name="date" />
  <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
  </div>
  </div>
  </div>

  <div class="row">
  <div class="col-md-6">
  <strong>MM</strong>
  <div class="input-group input-append date" id="datePickerMM">
  <input type="text" class="form-control" name="date" />
  <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
  </div>
  </div>

  <div class="col-md-6">
  <strong>mmyyyy</strong>

  <div class="input-group input-append date" id="datepickermmyyyy">
  <input type="text" class="form-control" name="date" />
  <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
  </div>
  </div>
  </div>
</body>
</html>

Jquery datepiker different format (dd/mm/yy OR Year only OR Month Only Or Year and Month only etc)

CSS and JS included  

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


DatePicker Day, Month and Year

HTML
  Date: <input type="text" id="datepicker">

CSS
No Change

Script
 <script>
  $(function () {
  $("#datepicker").datepicker(
  {
  changeYear: true,
  changeMonth: true,
  dateFormat: 'dd/mm/yy',
  });
  });
  </script>


Note [ mm/dd/yy like 15/10/2016, MM/dd/yy like October/20/2016]


DatePicker Month and Year Only

HTML
 <p>Date: <input type="text" id="datepicker"></p>

Css
-----
  .ui-datepicker-calendar {
  display: none;
  }

Script
--------

 <script>

  $(function () {
  $("#datepicker").datepicker(
  {
  changeMonth: true,
  changeYear: true,
  dateFormat: 'MM yy',
  onClose: function (dateText, inst) {
  var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
  var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
  $(this).datepicker('setDate', new Date(year, month, 1));
  }




  });
  });
  </script>


DatePicker Only Year
======================

HTML
-----

  <p>Date: <input type="text" class="class=" date-picker-year"" id="datepicker"></p>

css
----

 .ui-datepicker-calendar {
  display: none;
  }

  .ui-datepicker-month {
  display: none;
  }

  .ui-datepicker-prev {
  display: none;
  }

  .ui-datepicker-next {
  display: none;
  }

script
------

 <script>

  $(function () {
  $("#datepicker").datepicker(
  {
  changeYear: true,
  showButtonPanel: true,
  dateFormat: 'yy',
  onClose: function (dateText, inst) {
  var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
  $(this).datepicker('setDate', new Date(year, 1));
  }
  });
  $(".date-picker-year").focus(function () {
  $(".ui-datepicker-month").hide();

  });
  });
  </script>


Datepicker Only Month
======================

HTML
---------
 <p>Date: <input type="text" id="datepicker"></p>

CSS
-----
  <style>
  .MonthDatePicker .ui-datepicker-year {
  display: none;
  }

  .HideTodayButton .ui-datepicker-buttonpane .ui-datepicker-current {
  visibility: hidden;
  }

  .hide-calendar .ui-datepicker-calendar {
  display: none !important;
  visibility: hidden !important;
  }
  </style>


Script
-------
 <script>
  $(function () {
  $("#datepicker").datepicker({
  changeMonth: true,
  showButtonPanel: true,
  dateFormat: "M",
  beforeShow: function (e, t) {
  $(this).datepicker("hide");
  $("#ui-datepicker-div").addClass("hide-calendar");
  $("#ui-datepicker-div").addClass('MonthDatePicker');
  $("#ui-datepicker-div").addClass('HideTodayButton');
  },
  onClose: function(dateText, inst){
  var n = Math.abs($("#ui-datepicker-div .ui-datepicker-month :selected").val() - 1) + 2;
  $(this).datepicker("setDate", new Date(null, n, null));
  }
  });
  });
  </script>







Chosen dropdown and validation

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="openOffice.aspx.cs" Inherits="AngTEst.openOffice" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
//CSS and JS Start
    <link href="Assets/css/bootstrap.css" rel="stylesheet" />
    <link href="Assets/css/chosen.css" rel="stylesheet" />
    <script src="Assets/js/jquery.min.js"></script>
    <script src="Assets/js/bootstrap.js"></script>
    <script src="Assets/js/chosen.jquery.js"></script>
//CSS and JS End

//Script Start 

    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnSelect').click(function () {
                if ($('#ddlName').val() == 0) {
                    if ($("#ddlName").hasClass('chosen-select')) {
                        $("#ddlName_chosen").css("border", "1px solid red")
                    }

                }
                $('#ddlName').change(function () {
                    if ($('#ddlName').val() != 0) {
                        $("#ddlName_chosen").css("border", "0")
                    }
                });

                $('#ddlName').val(0);
                $('#ddlName').trigger("chosen:updated");


            });

        });
        $(function () {
            $(".chosen-select").chosen();
        });
    </script>
//Script End

</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
              <div class="row">
                <div class="col-md-6">
                    <strong>Name:</strong>
                    <select id="ddlName" class="form-control chosen-select">
                        <option value="0">Select</option>
                        <option value="1">Bhimsen</option>
                        <option value="2">Shiv</option>
                        <option value="3">Shakti</option>
                        <option value="4">Avnish</option>
                    </select>
                </div>

                <div class="col-md-6">
                    <input type="button" id="btnSelect" value="Reset" />
                </div>
            </div>
        </div>

    </form>
</body>
</html>

Wednesday, 12 October 2016

WedMethod wing Jquery Ajax function

 protected void Page_Load(object sender, EventArgs e)
        {

        }


        [WebMethod]
        public static string InsertData(string FirstName, string LastName, string Email, string Address)
        {
            string Retval = string.Empty;
          //  Do your task
            return Retval;
        }





<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AngTEst.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="scripts/jquery-1.10.2.min.js"></script>
    <link href="Assets/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <div class="row">

                <div class="col-md-6">
                    <div class="form-group">
                        <strong>First Name</strong>
                        <input type="text" id="txtFirstName" class="form-control validateCtrl" />
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <strong>Last Name</strong>
                        <input type="text" id="txtLastName" class="form-control validateCtrl" />
                    </div>
                </div>
            </div>
            <div class="row">

                <div class="col-md-6">
                    <div class="form-group">
                        <strong>First Name</strong>
                        <input type="text" id="txtEmail" class="form-control validateCtrl" />
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <strong>Last Name</strong>
                        <input type="text" id="txtAddress" class="form-control validateCtrl" />
                    </div>
                </div>
            </div>

            <div class="row">

                <div class="col-md-6">
                    <label id="lblmsg" style="font-weight: bold; color: Red" />
                </div>
                <div class="col-md-6">

                    <input type="button" id="btnSubmit" value="Submit" />
                </div>
            </div>

        </div>


    </form>

    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnSubmit').click(function () {
                var flag = false;

                $(".validateCtrl").each(function (index) {
                    if ($(this).val() == "") {
                        $(this).css("border", "1px solid red")
                        flag = true;
                    }
                    else {
                        $(this).css("border", "");
                    }                  
                });

                if (flag) {
                    $('#lblmsg').html("Please fill all required field");
                    return false;
                }
                else {
                    alert("validated")
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Default.aspx/InsertData",
                        data: "{'FirstName':'" + $('#txtFirstName').val() + "','LastName':'" + $('#txtLastName').val() + "','Email':'" + $('#txtEmail').val() + "','Address':'" + $('#txtAddress').val() + "'}",
                        dataType: "json",
                        success: function (data) {
                            var obj = data.d;
                            if (obj == 'true') {                              
                                $('#lblmsg').html("Details Submitted Successfully");
                            }
                        },
                        error: function (result) {
                            $('#lblmsg').html("Oops! somthing went wrong.");
                        }
                    });
                }

            })


        });


    </script>
</body>
</html>

Sunday, 9 October 2016

JQuery Captcha in ASP.NET

This sample demonstrates a simple way to use Jquery Captcha, Validate captcha with matching and Asp.net control validation using JQuery.


Captcha Code 

(function()  
{  
    var cx = '015363721894368854116:4i6ywnkggls';  
    var gcse = document.createElement('script');  
    gcse.type = 'text/javascript';  
    gcse.async = true;  
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +  
        '//www.google.com/cse/cse.js?cx=' + cx;  
    var s = document.getElementsByTagName('script')[0];  
    s.parentNode.insertBefore(gcse, s);  
})();  
  
function DrawCaptcha()  
{  
    var a = Math.ceil(Math.random() * 10) + '';  
    var b = Math.ceil(Math.random() * 10) + '';  
    var c = Math.ceil(Math.random() * 10) + '';  
    var d = Math.ceil(Math.random() * 10) + '';  
    var e = Math.ceil(Math.random() * 10) + '';  
    var f = Math.ceil(Math.random() * 10) + '';  
    var g = Math.ceil(Math.random() * 10) + '';  
    var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' ' + f + ' ' + g;  
    document.getElementById("txtCodeCaptcha").innerHTML = code  
}  
  
// Validate the Entered input aganist the generated security code function     
function ValidTestimonialCaptcha()  
{  
    var str1 = removeSpaces(document.getElementById('txtCodeCaptcha').innerHTML);  
    var str2 = removeSpaces(document.getElementById('TestimonialCAPTCHA').value);  
    if (str1 == str2) return true;  
    return false;  
  
}  
DrawCaptcha();  
  
  
function removeSpaces(str)  
{  
    return str = str.replace(/\s+/g, '');  
}  
  
  
  
  
Validation.js  
  
$(function()  
{  
    $('#reloadCaptcha').click(function()  
    {  
        DrawCaptcha();  
    });  
    $("#submitTestimonialsBtn").click(function()  
    {  
  
        if ($('#txtTestimonialName').val() == '')  
        {  
            $('#txtTestimonialName').prev('.msg').text('Please enter Name.').css('display', 'block');  
            $('#txtTestimonialName').focus();  
            return false;  
        }  
        else  
        {  
            $('#txtTestimonialName').prev('.msg').text('').css('display', 'none');  
        }  
        if ($("#txtTestimonialEmail").val() == "")  
        {  
            $('#txtTestimonialEmail').prev('.msg').text('Please enter an email.').css('display', 'block');  
            $('#txtTestimonialEmail').focus();  
            return false;  
        }  
        else  
        {  
            $("#txtTestimonialEmail").prev('.msg').text('').css('display', 'none');  
        }  
        if ($('#txtTestimonials').val() == "")  
        {  
            $('#txtTestimonials').prev('.msg').text('Please enter testimonial.').css('display', 'block');  
            $('#txtTestimonials').focus();  
            return false;  
        }  
        else  
        {  
            $("#txtTestimonials").prev('.msg').text('').css('display', 'none');  
        }  
        if ($('#TestimonialCAPTCHA').val() == "")  
        {  
            $('#TestimonialCAPTCHA').prev('.msg').text('Please enter valid captcha.').css('display', 'block');  
            $('#TestimonialCAPTCHA').focus();  
            return false;  
        }  
        else if (!ValidTestimonialCaptcha())  
        {  
            $('#TestimonialCAPTCHA').prev('.msg').text('Captcha does not match. Please enter valid captcha').css('display', 'block');  
            $('#TestimonialCAPTCHA').focus();  
            return false;  
        }  
        else  
        {  
            $("#TestimonialCAPTCHA").prev('.msg').text('').css('display', 'none');  
        }  
        $(this).submit();  
    });  
});  

Saturday, 8 October 2016

Jquery generic validation

Hi, In this article we are going to use generic validation for all the texbox and drodown. For this, we are using a specific class for all the control (.reqFeild) to get its value and validate if its is null or 0. I think this is very easy and simple method.

Code and Script are as follows :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <link href="content/bootstrap.min.css" rel="stylesheet" />
    <script src="content/jquery.min.js"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <strong>First Name : </strong>
                    <input type="text" class="form-control reqField" />
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <strong>Last Name : </strong>
                    <input type="text" class="form-control reqField" />
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <strong>Email : </strong>
                    <input type="text" class="form-control reqField" />
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <strong>Address : </strong>
                    <input type="text" class="form-control reqField" />
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <strong>Address : </strong>
                    <input type="text" class="form-control reqField" />
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <strong>Zip Code : </strong>
                    <input type="text" class="form-control reqField" />
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <strong>State : </strong>
                    <select class="form-control reqField">
                        <option value="0">Select</option>
                        <option value="1">Delhi</option>
                        <option value="2">Uttar Paradesh</option>
                        <option value="3">Bihar</option>
                        <option value="5">Hariyana</option>


                    </select>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <strong>Country : </strong>
                    <select class="form-control reqField">
                        <option value="0">Select</option>
                        <option value="1">India</option>
                        <option value="2">USA</option>
                        <option value="3">Pakistan</option>
                        <option value="4">Sri Lanka</option>


                    </select>
                </div>
            </div>
            <div class="row">

                <div class="col-md-6">
                    <input type="submit" id="btnSubmit" class="btn btn-primary" />
                </div>
                <div class="col-md-6">
                    <label id="lblError" style="color: red" class="hide">Please fill all the required fields.</label>
                </div>
            </div>
        </div>
    </div>
</body>

<script>
    $(document).ready(function () {

        $("#btnSubmit").click(function () {

            var flag = false;

            //Checking all the control using class .reqField. 0 for dropdown and "" for textbox validation
            $('.reqField').each(function () {
                if ($(this).val() == "0" || $(this).val() == "") {
                    $(this).css({ "border-color": "#ff0000" });
                    flag = true;
                }
                else {
                    $(this).css({ "border-color": "" });
                }
            });

            // checking flag value if true, lblerror msg will show else remove.
            if (flag) {
                $("#lblError").removeClass("hide")
                return false;
            }

            else {
                $("#lblError").addClass("hide")

            }

        });
    });

</script>

</html>

Hope, It is helpful to you. Thank You :)