HTML Validation

Integer Filter for Input Textbox in HTML

<!doctype html>
<html>
<head>
</head>
<body>
<input type=”text” onkeypress=”if(this.value.match(/\D/)) this.value=this.value.replace(/\D/g,”)”
onkeyup   =”if(this.value.match(/\D/)) this.value=this.value.replace(/\D/g,”)” name=”test” id=”test”  style=”width: 200px;” />
</body>
</html>

reference : http://www.javascripter.net/faq/keyboardinputfiltering.htm


Integer Filter for Input Textbox in HTML with JQUERY

<!doctype html>
<html>
<head>
</head>
<body>
<input type=”text” id=”test” name=”test” style=”width: 200px;” />
<input type=”text” id=”test2″ name=”test2″ style=”width: 200px;” />

<script>
$(document).ready(function() {
$(“#test,#test2”).keydown(function (event) {
// Prevent shift key since its not needed
if (event.shiftKey == true) {
event.preventDefault();
}
// Allow Only: keyboard 0-9, numpad 0-9, backspace, tab, left arrow, right arrow, delete
if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46) {
// Allow normal operation
} else {
// Prevent the rest
event.preventDefault();
}
});

});
</script>

</body>
</html>


Advertisements