Any quick way to set an HTML text input (<input type=text />
) to only allow numeric keystrokes(plus '.')?
views:
23223answers:
13I've used this, it's okay
http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
2 solutions:
Use a form validator (for example with jQuery validation plugin)
Do a check during the onblur (i.e. when the user leaves the field) event of the input field, with the regular expression:
<script type="text/javascript">
function testField(field) {
var regExpr = new RegExp("^\d*\.?\d*$");
if (!regExpr.test(field.value)) {
// Case of error
field.value = "";
}
}
</script>
<input type="text" ... onblur="testField(this);"/>
You may try using the '''onkeydown''' event and cancel the event (event.preventDefault or something like that) when it's not one of the allowed keys.
You can attach to the key down event and then filter keys according to what you need, for example:
<input id="FIELD_ID" name="FIELD_ID" onkeypress="return validateNUM(event,this);" type="text">
And the actual javascript handler would be:
function validateNUM(e,field)
{
var key = getKeyEvent(e)
if (specialKey(key)) return true;
if ((key >= 48 && key <= 57) || (key == 46)){
if (key != 46)
return true;
else{
if (field.value.search(/\./) == -1 && field.value.length > 0)
return true;
else
return false;
}
}
function getKeyEvent(e){
var keynum
var keychar
var numcheck
if(window.event) // IE
keynum = e.keyCode
else if(e.which) // Netscape/Firefox/Opera
keynum = e.which
return keynum;
}
Use this DOM
<input type='text' onkeypress='validate(event)' />
And this script
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]|\./;
if( !regex.test(key) ) {
theEvent.returnValue = false;
theEvent.preventDefault();
}
}
Remember the regional differences (Euros use periods and commas in the reverse way as Americans), plus the minus sign (or the convention of wrapping a number in parentheses to indicate negative), plus exponential notation (I'm reaching on that one).
Thanks guys this really help me!
I found the perfert one really useful for database.
function numonly(root){
var reet = root.value;
var arr1=reet.length;
var ruut = reet.charAt(arr1-1);
if (reet.length > 0){
var regex = /[0-9]|\./;
if (!ruut.match(regex)){
var reet = reet.slice(0, -1);
$(root).val(reet);
}
}
}
Then add the eventhandler:
onkeyup="numonly(this);"
And one more example, which works great for me:
function validateNumber(event) {
var key = window.event ? event.keyCode : event.which;
if (event.keyCode == 8 || event.keyCode == 46
|| event.keyCode == 37 || event.keyCode == 39) {
return true;
}
else if ( key < 48 || key > 57 ) {
return false;
}
else return true;
};
Also attach to keypress event
$(document).ready(function(){
$('[id^=edit]').keypress(validateNumber);
});
And html:
<input type="input" id="edit1" value="0" size="5" maxlength="5" />
wow everybody sends most important information. Thanks for sharing
<html>
<head>
<script>
function IsEmpty(aTextField)
{
if((aTextField.value.length==0) || (aTextField.value==null)) {return true;}
else{return false;}
}
function IsNumeric(sText)
{
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for(i=0;i<sText.length&&IsNumber==true;i++)
{
Char=sText.charAt(i);
if(ValidChars.indexOf(Char)==-1){IsNumber=false;}
}
return IsNumber;
}
function ValidateForm()
{
if(IsEmpty(document.getElementById("account_number")))
{
alert('You have not entered an account number')
document.getElementById("account_number").focus();
return false;
}
if(!IsNumeric(document.getElementById("account_number").value))
{
alert('Please enter only numbers or decimal points in the account field')
document.getElementById("account_number").focus();
document.getElementById("account_number").select();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="form1" action="#" method="post" onsubmit="return ValidateForm()">
Account Number: <input type="text" id="account_number" name="account_number">
<br />
<input type="submit" value="Submit">
</form>
</body>
</html>
If you want to suggest to the device(maybe a mobile phone) between alpha or numeric you can use <input type="number">
I've searched long and hard for a good answer to this, and we desperately need <input type="number"
, but short of that, these 2 are the most concise ways I could come up with:
<input type="text"
onkeyup="this.value=this.value.replace(/[^\d]/,'')">
If you dislike the non-accepted character showing for a split-second before being erased, the method below is my solution. Note the numerous additional conditions, this is to avoid disabling all sorts of navigation and hotkeys. If anyone knows how to compactify this, let us know!
<input type="text"
onkeydown="return ( event.ctrlKey || event.altKey
|| (47<event.keyCode && event.keyCode<58 && event.shiftKey==false)
|| (95<event.keyCode && event.keyCode<106)
|| (event.keyCode==8) || (event.keyCode==9)
|| (event.keyCode>34 && event.keyCode<40)
|| (event.keyCode==46) )">