Thursday, October 11, 2012

Validate input text field to number with two decimal places


HTML:

<input type="text" id="inputfieldid" maxlength="10"
onkeydown="ValidateInputNumber(this.value);"  onkeyup=" ValidateInputNumber(this.value);" onchange=" ValidateInputNumber(this.value);" />


Javascript:

 function ValidateInputNumber(input){

//You can specify any reg ex here to validate

 var pattern = new RegExp(/^[0-9]*(\.[0-9]?[0-9]?)?$/);
 var resultvalue = pattern.test(input);

 if(!resultvalue)
   {
     var inputlength = input.length;
     input = input.substring(0, inputlength-1);
     $('#inputfieldid').val(input);
    }
 }

No comments:

Post a Comment