Saturday, March 7, 2009

Validations for checking only numerics

The javascript below checks whether the user has entered numeric value or alphabets in the text box. Here we used the onkeyup event to raise the javascript event.


<!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=iso-8859-1">
<title>Untitled Document</title>
<script>
function checkForInvalid(obj) {
if( /[^0-9\-]|-{2,}/gi.test(obj.value) ) {
alert("Invalid character in Invoice No.")
obj.focus();
obj.select();
return false;
}
return true;
}

</script>
</head>

<body>
<form name="myForm">
<input type="text" name="invoiceNo" onkeyup="checkForInvalid(this)">
</form>
</body>
</html>

No comments:

Post a Comment