Allow Negative/Positive Numbers But Not Letters

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



Leave a reply



Submit