Categories
JavaScript Answers

How to restrict input to allow only numbers and decimal point in a textbox with JavaScript?

Spread the love

Sometimes, we want to restrict input to allow only numbers and decimal point in a textbox with JavaScript.

In this article, we’ll look at how to restrict input to allow only numbers and decimal point in a textbox with JavaScript.

How to restrict input to allow only numbers and decimal point in a textbox with JavaScript?

To restrict input to allow only numbers and decimal point in a textbox with JavaScript, we can call the string match method to validate the input value.

For instance, we write

form.onsubmit = () => {
  return textarea.value.match(/^\d+(\.\d+)?$/);
};

to set the onsubmit property of the form element object to a function that returns whether the textarea‘s input value is valid.

We call match with /^\d+(\.\d+)?$/ to check if the text area’s input value has digits and a decimal point.

Conclusion

To restrict input to allow only numbers and decimal point in a textbox with JavaScript, we can call the string match method to validate the input value.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *