Tuesday, May 18, 2010

JavaScript function to restrict input to numeric

1) Creating javascript file(util.js) and create function checkIt()
//only numbers
function onlyNum(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
status = "This field accepts numbers only."
return false
}
status = ""
return true
}

2) Include util.js file in jsp file

<script type='text/javascript' src="<%=request.getContextPath()%>/utils.js"></script>

3)Calling javascript function in jsp
<h:inputText id="tf_NAME" label="Name" autocomplete="off" onkeyup="return onlyNum(event);" >

No comments:

Post a Comment