Wednesday, May 19, 2010

JavaScript function to restrict input to only characters and space

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

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

No comments:

Post a Comment