Search This Blog

Thursday, July 13, 2017

JQuery - Positve/Negative Number(3 digits only) Max-Min-Range

<%@ Page Title="Test Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="TestPage.aspx.cs" Inherits="DeveloperTest01.About" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">    
        $(function () {      
            $("#btnsort").click(function () {
                var num1 = $('input:text[name=text0]').val();
                var num2 = $('input:text[name=text1]').val();
                var num3 = $('input:text[name=text2]').val();
                var num4 = $('input:text[name=text3]').val();
                var num5 = $('input:text[name=text4]').val();
                var num6 = $('input:text[name=text5]').val();
                var num7 = $('input:text[name=text6]').val();
                var num8 = $('input:text[name=text7]').val();
                var num9 = $('input:text[name=text8]').val();
                var points = [num1, num2, num3, num4, num5, num6, num7, num8, num9];
                points.sort(function (a, b) { return a - b });
                for (var i = 0; i < points.length; i++) {
                    $("input:text[name=text" + i + "]").val(points[i]);
                }
            });
            $("#btnPopulate").click(function () {
                var num1 = $('input:text[name=text0]').val();
                var num2 = $('input:text[name=text1]').val();
                var num3 = $('input:text[name=text2]').val();
                var num4 = $('input:text[name=text3]').val();
                var num5 = $('input:text[name=text4]').val();
                var num6 = $('input:text[name=text5]').val();
                var num7 = $('input:text[name=text6]').val();
                var num8 = $('input:text[name=text7]').val();
                var num9 = $('input:text[name=text8]').val();
                var _array = [num1, num2, num3, num4, num5, num6, num7, num8, num9];
               var max =  Math.max.apply(Math, _array); // 3
               var min = Math.min.apply(Math, _array); // 1

               document.getElementById("spnMax").innerHTML = max;
               document.getElementById("spnMMin").innerHTML = min;
               document.getElementById("spnRange").innerHTML = max - min;
               var sum = 0;
               var avg = 0;
               var num = 9;
               for (var i = 0; i < _array.length; i++) {
                   sum += Number(_array[i]);
               }            
               if (num > 0) { avg = Number(sum / num); }
               document.getElementById("spnMean").innerHTML = avg.toFixed(2);
            });        
         
            var t = false
            $('input').focus(function () {
                var $this = $(this)
                t = setInterval(
                function () {
           if (($this.val() < -999 || $this.val() > 999) && $this.val().length != 0) {
                        if ($this.val() < 1) {
                            $this.val("")
                        }
                        if ($this.val() > 999) {
                            $this.val("")
                        }
                        $('p').fadeIn(1000, function () {
                            $(this).fadeOut(500)
                        })
                    }


                }, 50)
            })
            $('input').blur(function () {
                if (t != false) {
                    window.clearInterval(t)
                    t = false;
                }
            })
            $('input').keypress(function (e) {
                //if the letter is not digit then display error and don't type anything
                if (e.which != 8 && e.which != 45 && (e.which < 48 || e.which > 57)) {
                    //display error message
                    $("#errmsg").html("Digits Only").show().fadeOut("slow");
                    return false;
                }
            });
        });
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
     <p style="display:none">Accepting Only values 3 digits</p>
    <table>
        <tr>
            <td>
                <input type="text" name="text0" id="text0" style="width: 50px"  value="" />
            </td>
            <td>
                <input type="text" name="text1"  id="text1" style="width: 50px" value="" />
            </td>
            <td>
                <input type="text" name="text2" id="text2" style="width: 50px" value="" />
            </td>
            <td>
                <input type="text" name="text3" id="text3" style="width: 50px" value="" />
            </td>
            <td>
                <input type="text" name="text4" id="text4" style="width: 50px" value="" />
            </td>
            <td>
                <input type="text" name="text5" id="text5" style="width: 50px" value="" />
            </td>
            <td>
                <input type="text" name="text6" id="text6" style="width: 50px" value="" />
            </td>
            <td>
                <input type="text" name="text7" id="text7" style="width: 50px" value="" />
            </td>
            <td>
                <input type="text" name="text8" id="text8" style="width: 50px" value="" />
            </td>
        </tr>
    </table>
    <input id="btnsort" name="btnsort" type="button" value="Sort" />
    <input id="btnPopulate" name="btnPopulate" type="button" value="Populate" />
    <br />  <br />
    Max : <span id="spnMax"></span><br />
    Min :  <span id="spnMMin"></span><br />
    Mean : <span id="spnMean"></span><br />
    Range : <span id="spnRange"></span><br />
</asp:Content>


No comments:

Post a Comment