I want to validate contact number field.That field allow only number.how to i validate use javascript

Recommended Answers

All 3 Replies

try this:

if(isNaN(d.number.value)){alert("Please Enter only Numbers in Phone no Feild");
	d.number.value="";d.number.focus();return false;}

This will validate your form and if the user try to input another value asside from 0-9 they wil be alerted for invalid entry! Enjoy your coding...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Only Numbers Are allowed</title>
<style type="text/css">
<!--

form { margin: 0 }

input[type=button] { background-color: #000000; color: #FFFFFF; }

-->
</style>
<script type='text/javascript'> 
<!-- BEGING HIDING

function isNumeric(id, yourMessage) 
{ 
var field = document.getElementById(id);
var numericExpression = /^[0-9]+$/; if( field.value.match( numericExpression ) )
{ alert('\nThank You\nYour number has been added.'); 
return true; 
}
else 
{ 
alert( yourMessage ); 
field.focus(); return false; 
  } 
} 
// DONE HIDING -->
</script>
</head>
<form> Phone#:<br /> 
<input type='text' id='numbers' size="18" />&nbsp; 
<input type='button' onclick="isNumeric('numbers', '\nInvalid phone n0.\nPlease try again')" value=' Enter ' /> 
</form>
</body> 
</html>
function iscontact()
{
var exp=/[0-9]/
if(document.forms[0].contact.value.match(exp)
document.forms[0].submit
else
alert("Please enter a valid contact no")
return false

I hope this will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.