Allow Only Numbers in Textbox Javascript

Home > Javascript Tutorial > Allow Only Numbers in Textbox Javascript



Allow numbers in textbox javascript and Restrict Alphabets and Special Characters

As you know sometimes when we talk about to allow only numbers and only alphabet or special character in textbox then we find difficulty. So, for solve out that problem here we will learn how to allow numbers in textbox javascript and how to restrict alphabets and special characters. There are a lot of websites where you can get this information but here, we will teach you with some easy and simple steps.

In this example , I will allow only numbers to be entered into textbox and if user enters with character it will warn the users. this below article will show you how to allow numbers in textbox ?

Eg

<!DOCYPTE html>
<html>
	<head>
		<title>javascript validation </title>
		<script>
			function chkNum(event,msg)
			{
				if(!(event.which>=48 && event.which<=57))
				{
				document.getElementById(msg).innerHTML="Invalid Mobile format";
				return false;
				}
				else
				{
				document.getElementById(msg).innerHTML="";
				return true;
				}
			}
		</script>
		<style>
			.err{color:red}
		</style>
	</head>
	
	<body>
		<table>
			<tr>
				<td>Enter Your Mobile Number</td>
				<td><input type="text" onkeypress="return chkNum(event,'error')"/></td>
				<td id="error" class="err"></td>
			</tr>
			
			
		</table>
	</body>
</html>
Output

Total Downloads : 2

Login / Register To Download