Tuesday, May 18, 2010

JavaScript function to restrict input to only characters

1) Creating javascript file(util.js) and create function onlyCharNoSpace()

//only characters
function onlyCharNoSpace(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
//alert(charCode)
if((charCode > 31 & charCode < 65) |(charCode > 90 & charCode < 97) |(charCode > 122 & charCode < 127)){
status = "This field accepts characters 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" onkeypress="return onlyCharNoSpace(event)" >

No comments:

Post a Comment