Allow negative/positive numbers but not letters
Try with following regexp:
/^-?[0-9]*$/
How to make type="number" to positive numbers only
Add a min
attribute
<input type="number" min="0">
javascript to allow only negative and positive numbers and decimal upto 6 digits on keypress
You could check the value with Regex:
var re = /^-?\d*\.?\d{0,6}$/;
var text = $(elem).val();
var isValid = (text.match(re) !== null);
The Regex means:
^ : beginning of string
-? : one or zero "-"
\d* : 0 to infinite numbers
\.? : 0 or 1 "."
\d{0,6} : from 0 to 6 numbers
$ : End of string
allow negative and positive number in textbox vb
Private Sub txtMonRenta_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtMonRenta.KeyPress
If Not IsNumeric(e.KeyChar) AndAlso System.Convert.ToByte(e.KeyChar) <> 8 AndAlso Convert.ToByte(e.KeyChar) <> 45 Then
e.Handled = True
End If
End Sub
this work perflecty!!
jquery - allow only negative, positive or decimal number validation in field
You can use regex instead to solve your problem, change your method to something like this:
function isNumbers(evt, element)
{
var elementValue = $(element).val();
var regex = /^(\+|-)?(\d*\.?\d*)$/;
if (regex.test(elementValue + String.fromCharCode(evt.charCode))) {
return true;
}
return false;
}
Related Topics
How to Prevent User to Enter Specific Key Using JavaScript
How to Open Specific Web Browsers Using Hyperlinks
Re-Render Same Component on Url Change in React
Javascript: Open Several Urls With One Button Click
Append Data to Json Array Issue
Creating a Multiple Choice Option in JavaScript
Can We Map Over Two Arrays At Same Time in Js
Most Efficient Method to Groupby on an Array of Objects
Regular Expression Using JavaScript for Removing the Special Characters
How to Add Active Class/State for Li Element in Reactjs
Detect If User Is Using Webview for Android/Ios or a Regular Browser
How to Clear the Focus in JavaScript
Force Download an Image from Src Using JavaScript
Getting Null Value in List When Passing by Ajax Call to MVC Controller
How to Merge Two Json Object Values by Id With Plain JavaScript (Es6)