Introduction :

In this article, we will see how to Allow Only Numbers and One Dot in Textbox jQuery Regex. This article will useful when we want to use the PRICE FIELD in the TEXTBOX. In this article, I just want to explain is that the user is not allowed to enter string value and allow only numeric and it also allows only ONE DOT.

Below is the example Allow Only Numbers and One Dot in Textbox jQuery you can understand easily.

So Let’s do this firstly we will declare the input tag with attribute class, type, and value.

<input type="text" class="decimalnumber" value="" />

Below is the code which is you have to use in JS. The main point regarding this is, it will check for multiple decimals, and also it will restrict the user to type only numbers.

$('.decimalnumber').keyup(function(){ var val = $(this).val(); if(isNaN(val)){ val = val.replace(/[^0-9\.]/g,''); if(val.split('.').length>2) val =val.replace(/\.+$/,""); } $(this).val(val); });​

With the help of this we can achieve Only Numbers and One Dot in Textbox using jQuery.

The detail explanation of the regex used above are as follow as:

  • Brackets means “any character (include also special character) inside these brackets.” You can use a hyphen (like above in the example) to indicate a range of chars.
  • And very important ,many time using the * means “zero or more of the previous expression.”
  • This is also one of the main point is that [0-9]* means “zero or more numbers”.
  • Backslash also a main thing in the regex is used as an escape character for the period, because period usually stands for “any character.”
  • This symbol that is Question mark “?” means “zero or one of the previous character.”
  • This symbol that is Upper arrow mark ^ represents the beginning of a string.
  • This symbol that is dollar mark $ represents the end of a string.
  • And last point is that starting the regex with ^ and ending with $ ensures that the entire string adheres to the regex pattern.

For other Query regarding jQuery you can go through this link : Related JQuery Question

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.