0

I use the following Script for Decimal validate. I need to enter only numbers and dot symbol like 123.00 only. The following function does not support letters that fine, but it does not allow to enter dot(.) symbol also? How to enter dot symbol using this function?

function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
         else
         return true;
      }
1

8 Answers 8

1
function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
Sign up to request clarification or add additional context in comments.

2 Comments

It was work fine. but its allow 123.23.45..... more than one dot.How to restrict to allow only one dot
Give id to input box, while validation, get the value of the textbox and split with ".", if array greater than 2, then return
0

Use regular expressions, its a more elegant method.

function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode.match('/^[-+]?([0-9]*\.[0-9]+|[0-9]+)$/'))
            return false;
         else
         return true;
      }

2 Comments

It gives the following error Microsoft JScript runtime error: Object doesn't support this property or method
Did you include the jQuery library? match() is a jQuery method.
0
function validateFloatKeyPress2(valor, presicion, event) {
        var keyCode = (document.all) ? event.keyCode : event.which;
        if (keyCode == 8) {
            return true;
        }            
        if (keyCode == 46 && valor.value.indexOf(".") >= 0) {               
            return false;
        }
        //Enable Only float values
        if ((keyCode >= 48 && keyCode <= 57) || (keyCode == 46)) {

            if (reverse(valor.value).indexOf(".") > (presicion - 1)) {
                return false;
            }
            return true;
        }
        else {
            return false;
        }
    }

function reverse(s) {
        var o = '';
        for (var i = s.length - 1; i >= 0; i--)
            o += s[i];
        return o;
    }

1 Comment

Accepts decimal numbers, for IE and Firefox
0

Try this:

//Validating Numeric Fields

function validate(x) {
    var decimal = /^[1-9][\.\d]*(,\d+)?$/;
    if (x.match(decimal)) {
        return true;
    } else {
        return false;
    }
}

Also you can check parseFloat method and that returns NaN.

Comments

0

check thorugh isNaN() method

var a =3.00003

isNaN(a) will return false

var b ='3.0.0' isNaN(b) will return true

i think this method will solve your problem.

Comments

0
      function checkDecimal(num){   

        var chkDecimal=  /^\d+\.\d{0,2}$/;
        return  chkDecimal.test(num);

      }

      alert(checkDecimal(0.2)) 
      alert(checkDecimal(1))  

Comments

0

Complete solution for your problem is

function isNumberKey(e)
    {
        var code = (code ? code : e.which);           
        if (code != 46 && code > 31 && (code < 48 || code > 57))
            return false;
            //if it is (.)
        else if (code == 46) {
             var Value = this.value;
            //if value already contains (.) character
            if (Value.indexOf('.') != -1) {
                var splt = Value.split('.');
                //if there is already(.) char then return false
                if (splt.length >= 2)
                    return false;
            }
        }
        return true;
    }

Comments

0
function IsNumeric(e) {
        var keyCode = e.which ? e.which : e.keyCode;
        var ret = ((keyCode == 46 || keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
        return ret;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.