I'm trying to get my login form to only validate if only numbers were inputted. I can it to work if the input is only digits, but when i type any characters after a number, it will still validate etc. 12akf will work. 1am will work. How can i get past this?
Part of the Login
<form name="myForm">
<label for="firstname">Age: </label>
<input name="num" type="text" id="username" size="1">
<input type="submit" value="Login" onclick="return validateForm()">
function validateForm()
{
var z = document.forms["myForm"]["num"].value;
if(!z.match(/^\d+/))
{
alert("Please only enter numeric characters only for your Age! (Allowed input:0-9)")
}
}
this function will allow only numbers in the textfield.
Javascript Approach
jQuery Approach
The above answers are for most common use case - validating input as a number.
// I use this jquery it works perfect, just add class nosonly to any textbox that should be numbers only:
This one worked for me :
Regular expressions are great, but why not just make sure it's a number before trying to do something with it?
here is how to validate the input to only accept numbers this will accept numbers like 123123123.41212313
and this will not accept entering the dot (.), so it will only accept integers
this way you will not permit the user to input anything but numbers