You must Sign In to post a response.
  • Category: JavaScript

    Negative and positive decimal validation

    Onkey up OR Onkey down, validation required

    Allow only Negative and positive decimal value,
    ex
    -2.2
    -2,
    3.25

    using jquery ,javascript
  • #753462
    [Response removed by Admin. Read forum policies.]

  • #753467
    Hi,

    Refer below sample javascript and call this on your control.


    <script type="text/javascript">
    function IsNumFieldKeyPress() {
    var element;
    element = event.srcElement;
    if (event.keyCode >= 48 && event.keyCode <= 57) {//to handle num 0 -9
    event.returnValue = true;
    return;
    }
    else if (event.keyCode == 46) { // to handle "."
    if (element.value.indexOf('.', 0) == -1) {
    event.returnValue = true;
    return;
    }
    }
    else if (event.keyCode == 43) { // to handle +
    if (element.value == undefined)
    element.value = "+";
    else if (element.value.charAt(0) != "+" && element.value.charAt(0) != "-")
    element.value = "+" + element.value;
    }
    else if (event.keyCode == 45) { // to handle -
    if (element.value == undefined)
    element.value = "-";
    else if (element.value.charAt(0) != "-" && element.value.charAt(0) != "+")
    element.value = "-" + element.value;
    }
    event.returnValue = false;
    }
    </script>



    REfer below link for more details.

    http://www.dotnetspider.com/resources/44902-How-Handle-numbers-Using-JavaScript.aspx

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #753470
    You can use the below Regular experession to validate the value.


    ^-?[0-9]\d*(\.\d+)?$

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #753514
    You can use Regex or a javascript inbuilt method to validate the things
    see below snippet

    function validateNumbers()
    {
    var re = /^-?\d*\.?\d+$/;
    var szValue = document.getElementById("txtNumber").value;
    if(re.test(szValue))
    alert("Entered numbers are allowed");
    else
    alert("Entered numbers are not allowed")
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments