Skip to content

Instantly share code, notes, and snippets.

@akshayvinchurkar
Last active May 14, 2019 07:57
Show Gist options
  • Save akshayvinchurkar/f862b5db153a103aa624c5a992bc8612 to your computer and use it in GitHub Desktop.
Save akshayvinchurkar/f862b5db153a103aa624c5a992bc8612 to your computer and use it in GitHub Desktop.
javascript input validations (client side)
// function to get only numbers in input field
function validateKeyStrokes(event) {
var charCode = (event.which) ? event.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
// use like this onkeypress="return validateKeyStrokes(event)"
// function to get only text in input field
function onlyAlphabets(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
return true;
else
return false;
}
catch (err) {
alert(err.Description);
}
}
// use like this onkeypress="return onlyAlphabets(event, this)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment