Wednesday, May 19, 2010

JavaScript function to restrict SpecialCharacter

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

//for NoSpecialCharacter
function NoSpecialCharacter(evt) {

evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
//alert(charCode)
if((charCode > 30 & charCode < 48)|charCode==32 |(charCode > 57 & charCode < 65) |(charCode > 90 & charCode < 97)|(charCode > 122 & charCode < 127) ){
status = "This field accepts No Special Character."
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 NoSpecialCharacter(event);" >

No comments:

Post a Comment